home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / lisp / clx_tar.z / clx_tar / clx / dependent.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1993-02-05  |  123.3 KB  |  3,723 lines

  1. ;;; -*- Mode: LISP; Syntax: Common-lisp; Package: XLIB; Base: 10; Lowercase: Yes -*-
  2.  
  3. ;; This file contains some of the system dependent code for CLX
  4.  
  5. ;;;
  6. ;;;             TEXAS INSTRUMENTS INCORPORATED
  7. ;;;                  P.O. BOX 2909
  8. ;;;                   AUSTIN, TEXAS 78769
  9. ;;;
  10. ;;; Copyright (C) 1987 Texas Instruments Incorporated.
  11. ;;;
  12. ;;; Permission is granted to any individual or institution to use, copy, modify,
  13. ;;; and distribute this software, provided that this complete copyright and
  14. ;;; permission notice is maintained, intact, in all copies and supporting
  15. ;;; documentation.
  16. ;;;
  17. ;;; Texas Instruments Incorporated provides this software "as is" without
  18. ;;; express or implied warranty.
  19. ;;;
  20.  
  21. (in-package :xlib)
  22.  
  23. #+cmu16
  24. (setf (getf ext:*herald-items* :xlib)
  25.       `("    CLX X Library " ,*version*))
  26.  
  27. ;;; The size of the output buffer.  Must be a multiple of 4.
  28. (defparameter *output-buffer-size* 8192)
  29.  
  30. #+explorer
  31. (zwei:define-indentation event-case (1 1))
  32.  
  33. ;;; Number of seconds to wait for a reply to a server request
  34. (defparameter *reply-timeout* nil) 
  35.  
  36. #-(or clx-overlapping-arrays (not clx-little-endian))
  37. (progn
  38.   (defconstant *word-0* 0)
  39.   (defconstant *word-1* 1)
  40.  
  41.   (defconstant *long-0* 0)
  42.   (defconstant *long-1* 1)
  43.   (defconstant *long-2* 2)
  44.   (defconstant *long-3* 3))
  45.  
  46. #-(or clx-overlapping-arrays clx-little-endian)
  47. (progn
  48.   (defconstant *word-0* 1)
  49.   (defconstant *word-1* 0)
  50.  
  51.   (defconstant *long-0* 3)
  52.   (defconstant *long-1* 2)
  53.   (defconstant *long-2* 1)
  54.   (defconstant *long-3* 0))
  55.  
  56. ;;; Set some compiler-options for often used code
  57.  
  58. (eval-when (eval compile load)
  59.  
  60. (defconstant *buffer-speed* 3
  61.   "Speed compiler option for buffer code.")
  62. (defconstant *buffer-safety* #+clx-debugging 3 #-clx-debugging 0
  63.   "Safety compiler option for buffer code.")
  64.  
  65. )
  66.  
  67. (eval-when (eval compile load)
  68.  
  69. (defun declare-bufmac ()
  70.   `(declare (optimize (speed ,*buffer-speed*) (safety ,*buffer-safety*))))
  71.  
  72. ;;; It's my impression that in lucid there's some way to make a declaration
  73. ;;; called fast-entry or something that causes a function to not do some
  74. ;;; checking on args. Sadly, we have no lucid manuals here.  If such a
  75. ;;; declaration is available, it would be a good idea to make it here when
  76. ;;; *buffer-speed* is 3 and *buffer-safety* is 0.
  77. (defun declare-buffun ()
  78.   #+(and cmu clx-debugging)
  79.   '(declare (optimize (speed 1) (safety 1)))
  80.   #-(and cmu clx-debugging)
  81.   `(declare (optimize (speed ,*buffer-speed*) (safety ,*buffer-safety*))))
  82.  
  83. )
  84.  
  85. (declaim (inline card8->int8 int8->card8
  86.          card16->int16 int16->card16
  87.          card32->int32 int32->card32))
  88.  
  89. #-Genera
  90. (progn
  91.  
  92. (defun card8->int8 (x)
  93.   (declare (type card8 x))
  94.   (declare (values int8))
  95.   #.(declare-buffun)
  96.   (the int8 (if (logbitp 7 x)
  97.         (the int8 (- x #x100))
  98.           x)))
  99.  
  100. (defun int8->card8 (x)
  101.   (declare (type int8 x))
  102.   (declare (values card8))
  103.   #.(declare-buffun)
  104.   (the card8 (ldb (byte 8 0) x)))
  105.  
  106. (defun card16->int16 (x)
  107.   (declare (type card16 x))
  108.   (declare (values int16))
  109.   #.(declare-buffun)
  110.   (the int16 (if (logbitp 15 x)
  111.          (the int16 (- x #x10000))
  112.          x)))
  113.  
  114. (defun int16->card16 (x)
  115.   (declare (type int16 x))
  116.   (declare (values card16))
  117.   #.(declare-buffun)
  118.   (the card16 (ldb (byte 16 0) x)))
  119.  
  120. #-akcl
  121. (defun card32->int32 (x)
  122.   (declare (type card32 x))
  123.   (declare (values int32))
  124.   #.(declare-buffun)
  125.   (the int32 (if (logbitp 31 x)
  126.          (the int32 (- x #x100000000))
  127.          x)))
  128.  
  129. #+akcl
  130. (defun card32->int32 (x)
  131.   (declare (type card32 x))
  132.   (declare (values int32))
  133.   (if (typep x 'fixnum)
  134.       x
  135.       (the int32 (if (logbitp 31 x)
  136.              (the int32 (- x #x100000000))
  137.              x))))
  138.   
  139. (defun int32->card32 (x)
  140.   (declare (type int32 x))
  141.   (declare (values card32))
  142.   #.(declare-buffun)
  143.   (the card32 (ldb (byte 32 0) x)))
  144.  
  145. )
  146.  
  147. #+Genera
  148. (progn
  149.  
  150. (defun card8->int8 (x)
  151.   (declare lt:(side-effects simple reducible))
  152.   (if (logbitp 7 x) (- x #x100) x))
  153.  
  154. (defun int8->card8 (x)
  155.   (declare lt:(side-effects simple reducible))
  156.   (ldb (byte 8 0) x))
  157.  
  158. (defun card16->int16 (x)
  159.   (declare lt:(side-effects simple reducible))
  160.   (if (logbitp 15 x) (- x #x10000) x))
  161.  
  162. (defun int16->card16 (x)
  163.   (declare lt:(side-effects simple reducible))
  164.   (ldb (byte 16 0) x))
  165.  
  166. (defun card32->int32 (x)
  167.   (declare lt:(side-effects simple reducible))
  168.   (sys:%logldb (byte 32 0) x))
  169.  
  170. (defun int32->card32 (x)
  171.   (declare lt:(side-effects simple reducible))
  172.   (ldb (byte 32 0) x))
  173.  
  174. )
  175.  
  176. (declaim (inline aref-card8 aset-card8 aref-int8 aset-int8))
  177.  
  178. #-(or Genera lcl3.0 excl akcl)
  179. (progn
  180.  
  181. (defun aref-card8 (a i)
  182.   (declare (type buffer-bytes a)
  183.        (type array-index i))
  184.   (declare (values card8))
  185.   #.(declare-buffun)
  186.   (the card8 (aref a i)))
  187.  
  188. (defun aset-card8 (v a i)
  189.   (declare (type card8 v)
  190.        (type buffer-bytes a)
  191.        (type array-index i))
  192.   #.(declare-buffun)
  193.   (setf (aref a i) v))
  194.  
  195. (defun aref-int8 (a i)
  196.   (declare (type buffer-bytes a)
  197.        (type array-index i))
  198.   (declare (values int8))
  199.   #.(declare-buffun)
  200.   (card8->int8 (aref a i)))
  201.  
  202. (defun aset-int8 (v a i)
  203.   (declare (type int8 v)
  204.        (type buffer-bytes a)
  205.        (type array-index i))
  206.   #.(declare-buffun)
  207.   (setf (aref a i) (int8->card8 v)))
  208.  
  209. )
  210.  
  211. #+Genera
  212. (progn
  213.  
  214. (defun aref-card8 (a i)
  215.   (aref a i))
  216.  
  217. (defun aset-card8 (v a i)
  218.   (zl:aset v a i))
  219.  
  220. (defun aref-int8 (a i)
  221.   (card8->int8 (aref a i)))
  222.  
  223. (defun aset-int8 (v a i)
  224.   (zl:aset (int8->card8 v) a i))
  225.  
  226. )
  227.  
  228. #+(or excl lcl3.0 clx-overlapping-arrays)
  229. (declaim (inline aref-card16 aref-int16 aref-card32 aref-int32 aref-card29
  230.          aset-card16 aset-int16 aset-card32 aset-int32 aset-card29))
  231.  
  232. #+(and clx-overlapping-arrays Genera)
  233. (progn
  234.  
  235. (defun aref-card16 (a i)
  236.   (aref a i))
  237.  
  238. (defun aset-card16 (v a i)
  239.   (zl:aset v a i))
  240.  
  241. (defun aref-int16 (a i)
  242.   (card16->int16 (aref a i)))
  243.  
  244. (defun aset-int16 (v a i)
  245.   (zl:aset (int16->card16 v) a i)
  246.   v)
  247.  
  248. (defun aref-card32 (a i)
  249.   (int32->card32 (aref a i)))
  250.  
  251. (defun aset-card32 (v a i)
  252.   (zl:aset (card32->int32 v) a i))
  253.  
  254. (defun aref-int32 (a i) (aref a i))
  255.  
  256. (defun aset-int32 (v a i)
  257.   (zl:aset v a i))
  258.  
  259. (defun aref-card29 (a i)
  260.   (aref a i))
  261.  
  262. (defun aset-card29 (v a i)
  263.   (zl:aset v a i))
  264.  
  265. )
  266.  
  267. #+(and clx-overlapping-arrays (not Genera))
  268. (progn
  269.  
  270. (defun aref-card16 (a i)
  271.   (aref a i))
  272.  
  273. (defun aset-card16 (v a i)
  274.   (setf (aref a i) v))
  275.  
  276. (defun aref-int16 (a i)
  277.   (card16->int16 (aref a i)))
  278.  
  279. (defun aset-int16 (v a i)
  280.   (setf (aref a i) (int16->card16 v))
  281.   v)
  282.  
  283. (defun aref-card32 (a i)
  284.   (aref a i))
  285.  
  286. (defun aset-card32 (v a i)
  287.   (setf (aref a i) v))
  288.  
  289. (defun aref-int32 (a i)
  290.   (card32->int32 (aref a i)))
  291.  
  292. (defun aset-int32 (v a i)
  293.   (setf (aref a i) (int32->card32 v))
  294.   v)
  295.  
  296. (defun aref-card29 (a i)
  297.   (aref a i))
  298.  
  299. (defun aset-card29 (v a i)
  300.   (setf (aref a i) v))
  301.  
  302. )
  303.  
  304. #+excl
  305. (progn
  306.   
  307. (defun aref-card8 (a i)
  308.   (declare (type buffer-bytes a)
  309.        (type array-index i))
  310.   (declare (values card8))
  311.   #.(declare-buffun)
  312.   (the card8 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  313.              :unsigned-byte)))
  314.  
  315. (defun aset-card8 (v a i)
  316.   (declare (type card8 v)
  317.        (type buffer-bytes a)
  318.        (type array-index i))
  319.   #.(declare-buffun)
  320.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  321.             :unsigned-byte) v))
  322.  
  323. (defun aref-int8 (a i)
  324.   (declare (type buffer-bytes a)
  325.        (type array-index i))
  326.   (declare (values int8))
  327.   #.(declare-buffun)
  328.   (the int8 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  329.             :signed-byte)))
  330.  
  331. (defun aset-int8 (v a i)
  332.   (declare (type int8 v)
  333.        (type buffer-bytes a)
  334.        (type array-index i))
  335.   #.(declare-buffun)
  336.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  337.             :signed-byte) v))
  338.  
  339. (defun aref-card16 (a i)
  340.   (declare (type buffer-bytes a)
  341.        (type array-index i))
  342.   (declare (values card16))
  343.   #.(declare-buffun)
  344.   (the card16 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  345.               :unsigned-word)))
  346.   
  347. (defun aset-card16 (v a i)
  348.   (declare (type card16 v)
  349.        (type buffer-bytes a)
  350.        (type array-index i))
  351.   #.(declare-buffun)
  352.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  353.             :unsigned-word) v))
  354.   
  355. (defun aref-int16 (a i)
  356.   (declare (type buffer-bytes a)
  357.        (type array-index i))
  358.   (declare (values int16))
  359.   #.(declare-buffun)
  360.   (the int16 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  361.              :signed-word)))
  362.   
  363. (defun aset-int16 (v a i)
  364.   (declare (type int16 v)
  365.        (type buffer-bytes a)
  366.        (type array-index i))
  367.   #.(declare-buffun)
  368.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  369.             :signed-word) v))
  370.   
  371. (defun aref-card32 (a i)
  372.   (declare (type buffer-bytes a)
  373.        (type array-index i))
  374.   (declare (values card32))
  375.   #.(declare-buffun)
  376.   (the card32 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  377.               :unsigned-long)))
  378.     
  379. (defun aset-card32 (v a i)
  380.   (declare (type card32 v)
  381.        (type buffer-bytes a)
  382.        (type array-index i))
  383.   #.(declare-buffun)
  384.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  385.             :unsigned-long) v))
  386.  
  387. (defun aref-int32 (a i)
  388.   (declare (type buffer-bytes a)
  389.        (type array-index i))
  390.   (declare (values int32))
  391.   #.(declare-buffun)
  392.   (the int32 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  393.              :signed-long)))
  394.     
  395. (defun aset-int32 (v a i)
  396.   (declare (type int32 v)
  397.        (type buffer-bytes a)
  398.        (type array-index i))
  399.   #.(declare-buffun)
  400.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  401.             :signed-long) v))
  402.  
  403. (defun aref-card29 (a i)
  404.   (declare (type buffer-bytes a)
  405.        (type array-index i))
  406.   (declare (values card29))
  407.   #.(declare-buffun)
  408.   (the card29 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  409.               :unsigned-long)))
  410.  
  411. (defun aset-card29 (v a i)
  412.   (declare (type card29 v)
  413.        (type buffer-bytes a)
  414.        (type array-index i))
  415.   #.(declare-buffun)
  416.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  417.             :unsigned-long) v))
  418.   
  419. )
  420.  
  421. #+lcl3.0
  422. (progn
  423.  
  424. (defun aref-card8 (a i)
  425.   (declare (type buffer-bytes a)
  426.        (type array-index i)
  427.        (values card8))
  428.   #.(declare-buffun)
  429.   (the card8 (lucid::%svref-8bit a i)))
  430.  
  431. (defun aset-card8 (v a i)
  432.   (declare (type card8 v)
  433.        (type buffer-bytes a)
  434.        (type array-index i))
  435.   #.(declare-buffun)
  436.   (setf (lucid::%svref-8bit a i) v))
  437.  
  438. (defun aref-int8 (a i)
  439.   (declare (type buffer-bytes a)
  440.        (type array-index i)
  441.        (values int8))
  442.   #.(declare-buffun)
  443.   (the int8 (lucid::%svref-signed-8bit a i)))
  444.  
  445. (defun aset-int8 (v a i)
  446.   (declare (type int8 v)
  447.        (type buffer-bytes a)
  448.        (type array-index i))
  449.   #.(declare-buffun)
  450.   (setf (lucid::%svref-signed-8bit a i) v))
  451.  
  452. (defun aref-card16 (a i)
  453.   (declare (type buffer-bytes a)
  454.        (type array-index i)
  455.        (values card16))
  456.   #.(declare-buffun)
  457.   (the card16 (lucid::%svref-16bit a (index-ash i -1))))
  458.   
  459. (defun aset-card16 (v a i)
  460.   (declare (type card16 v)
  461.        (type buffer-bytes a)
  462.        (type array-index i))
  463.   #.(declare-buffun)
  464.   (setf (lucid::%svref-16bit a (index-ash i -1)) v))
  465.   
  466. (defun aref-int16 (a i)
  467.   (declare (type buffer-bytes a)
  468.        (type array-index i)
  469.        (values int16))
  470.   #.(declare-buffun)
  471.   (the int16 (lucid::%svref-signed-16bit a (index-ash i -1))))
  472.   
  473. (defun aset-int16 (v a i)
  474.   (declare (type int16 v)
  475.        (type buffer-bytes a)
  476.        (type array-index i))
  477.   #.(declare-buffun)
  478.   (setf (lucid::%svref-signed-16bit a (index-ash i -1)) v))
  479.  
  480. (defun aref-card32 (a i)
  481.   (declare (type buffer-bytes a)
  482.        (type array-index i)
  483.        (values card32))
  484.   #.(declare-buffun)
  485.   (the card32 (lucid::%svref-32bit a (index-ash i -2))))
  486.     
  487. (defun aset-card32 (v a i)
  488.   (declare (type card32 v)
  489.        (type buffer-bytes a)
  490.        (type array-index i))
  491.   #.(declare-buffun)
  492.   (setf (lucid::%svref-32bit a (index-ash i -2)) v))
  493.  
  494. (defun aref-int32 (a i)
  495.   (declare (type buffer-bytes a)
  496.        (type array-index i)
  497.        (values int32))
  498.   #.(declare-buffun)
  499.   (the int32 (lucid::%svref-signed-32bit a (index-ash i -2))))
  500.     
  501. (defun aset-int32 (v a i)
  502.   (declare (type int32 v)
  503.        (type buffer-bytes a)
  504.        (type array-index i))
  505.   #.(declare-buffun)
  506.   (setf (lucid::%svref-signed-32bit a (index-ash i -2)) v))
  507.  
  508. (defun aref-card29 (a i)
  509.   (declare (type buffer-bytes a)
  510.        (type array-index i)
  511.        (values card29))
  512.   #.(declare-buffun)
  513.   (the card29 (lucid::%svref-32bit a (index-ash i -2))))
  514.  
  515. (defun aset-card29 (v a i)
  516.   (declare (type card29 v)
  517.        (type buffer-bytes a)
  518.        (type array-index i))
  519.   #.(declare-buffun)
  520.   (setf (lucid::%svref-32bit a (index-ash i -2)) v))
  521.  
  522. )
  523.  
  524. #+akcl
  525. (progn
  526.  
  527. (defmacro def-inline (name args return-type flags string)
  528.   (let* ((inline (list args return-type  flags string)))
  529.     `(car (push ',inline (get ',name 'compiler::inline-always)))))
  530.  
  531. (defmacro defun-inline (name args return-type flags string)
  532.   (let* ((sym (gensym))
  533.          (named-args
  534.           (nthcdr (- 10 (length args)) '(X9 X8 X7 X6 X5 X4 X3 X2 X1 X0)))
  535.          (inline (eval `(def-inline ,sym ,args ,return-type ,flags ,string))))
  536.     `(progn
  537.        (defun ,name  ,named-args
  538.          (declare ,@ (sloop::sloop for v in named-args for w in args
  539.                             when (not (eq t v))
  540.                             collect (list w v)))
  541.          (the ,return-type (,sym ,@ named-args)))
  542.        (push  ',inline (get ',name 'compiler::inline-always)))))
  543.  
  544. #+helper
  545. `(progn ,@
  546.       (sloop::sloop 
  547.  for v in '(card29 int8 card8  int16 card16 int32)
  548.  for w in '("unsigned long"
  549.         "char" "unsigned char" "short" "unsigned short" "long")
  550.  for name = (intern (format nil "AREF-~a"v))
  551.  for name-set = (intern (format nil "ASET-~a"v))
  552.  collect
  553.  `(defun-inline  ,name (t fixnum) fixnum #.(compiler::flags compiler::rfa)
  554.      ,(format nil "(*(~a *)(&((#0)->ust.ust_self[#1])))" w))
  555.  collect
  556.  `(defun-inline ,name-set (fixnum t fixnum) fixnum
  557.     #.(compiler::flags set compiler::rfa)
  558.      ,(format nil "(*(~a *)(&((#1)->ust.ust_self[#2])))=(~a)(#0)" w w))))
  559. (PROGN
  560.   (DEFUN-INLINE AREF-CARD29 (T FIXNUM) FIXNUM 8
  561.       "(*(unsigned long *)(&((#0)->ust.ust_self[#1])))")
  562.   (DEFUN-INLINE ASET-CARD29 (FIXNUM T FIXNUM) FIXNUM 10
  563.       "(*(unsigned long *)(&((#1)->ust.ust_self[#2])))=(unsigned long)(#0)")
  564.   (DEFUN-INLINE AREF-INT8 (T FIXNUM) FIXNUM 8
  565.       "(*(char *)(&((#0)->ust.ust_self[#1])))")
  566.   (DEFUN-INLINE ASET-INT8 (FIXNUM T FIXNUM) FIXNUM 10
  567.       "(*(char *)(&((#1)->ust.ust_self[#2])))=(char)(#0)")
  568.   (DEFUN-INLINE AREF-CARD8 (T FIXNUM) FIXNUM 8
  569.       "(*(unsigned char *)(&((#0)->ust.ust_self[#1])))")
  570.   (DEFUN-INLINE ASET-CARD8 (FIXNUM T FIXNUM) FIXNUM 10
  571.       "(*(unsigned char *)(&((#1)->ust.ust_self[#2])))=(unsigned char)(#0)")
  572.   (DEFUN-INLINE AREF-INT16 (T FIXNUM) FIXNUM 8
  573.       "(*(short *)(&((#0)->ust.ust_self[#1])))")
  574.   (DEFUN-INLINE ASET-INT16 (FIXNUM T FIXNUM) FIXNUM 10
  575.       "(*(short *)(&((#1)->ust.ust_self[#2])))=(short)(#0)")
  576.   (DEFUN-INLINE AREF-CARD16 (T FIXNUM) FIXNUM 8
  577.       "(*(unsigned short *)(&((#0)->ust.ust_self[#1])))")
  578.   (DEFUN-INLINE ASET-CARD16 (FIXNUM T FIXNUM) FIXNUM 10
  579.       "(*(unsigned short *)(&((#1)->ust.ust_self[#2])))=(unsigned short)(#0)")
  580.   (DEFUN-INLINE AREF-INT32 (T FIXNUM) FIXNUM 8
  581.       "(*(long *)(&((#0)->ust.ust_self[#1])))")
  582.   (DEFUN-INLINE ASET-INT32 (FIXNUM T FIXNUM) FIXNUM 10
  583.       "(*(long *)(&((#1)->ust.ust_self[#2])))=(long)(#0)"))
  584.  
  585. (defun aref-card32 (a i)
  586.   (declare (type buffer-bytes a)
  587.        (type array-index i))
  588.   (declare (values card32))
  589.   #.(declare-buffun)
  590.   (the card32
  591.        (logior (the card32
  592.             (ash (the card8 (aref a (index+ i *long-3*))) 24))
  593.            (the card29
  594.             (ash (the card8 (aref a (index+ i *long-2*))) 16))
  595.            (the card16
  596.             (ash (the card8 (aref a (index+ i *long-1*))) 8))
  597.            (the card8
  598.             (aref a (index+ i *long-0*))))))
  599.  
  600. (defun aset-card32 (v a i &aux (h 0))
  601.   (declare (type card32 v)
  602.        (type buffer-bytes a)
  603.        (type array-index i)
  604.        (type int32 h))
  605.   (if (typep v 'int32)
  606.       (setq h v)
  607.       (setq h (card32->int32 v)))
  608.   (aset-card29 h a i)
  609.   ;(or (eql v (aref-card32 a i)) (print (list me (aref-card32 a i))))
  610.   v)
  611.  
  612. )
  613.  
  614. #-(or excl lcl3.0 akcl clx-overlapping-arrays)
  615. (progn
  616.  
  617. (defun aref-card16 (a i)
  618.   (declare (type buffer-bytes a)
  619.        (type array-index i))
  620.   (declare (values card16))
  621.   #.(declare-buffun)
  622.   (the card16
  623.        (logior (the card16
  624.             (ash (the card8 (aref a (index+ i *word-1*))) 8))
  625.            (the card8
  626.             (aref a (index+ i *word-0*))))))
  627.  
  628. (defun aset-card16 (v a i)
  629.   (declare (type card16 v)
  630.        (type buffer-bytes a)
  631.        (type array-index i))
  632.   #.(declare-buffun)
  633.   (setf (aref a (index+ i *word-1*)) (the card8 (ldb (byte 8 8) v))
  634.     (aref a (index+ i *word-0*)) (the card8 (ldb (byte 8 0) v)))
  635.   v)
  636.  
  637. (defun aref-int16 (a i)
  638.   (declare (type buffer-bytes a)
  639.        (type array-index i))
  640.   (declare (values int16))
  641.   #.(declare-buffun)
  642.   (the int16
  643.        (logior (the int16
  644.             (ash (the int8 (aref-int8 a (index+ i *word-1*))) 8))
  645.            (the card8
  646.             (aref a (index+ i *word-0*))))))
  647.  
  648. (defun aset-int16 (v a i)
  649.   (declare (type int16 v)
  650.        (type buffer-bytes a)
  651.        (type array-index i))
  652.   #.(declare-buffun)
  653.   (setf (aref a (index+ i *word-1*)) (the card8 (ldb (byte 8 8) v))
  654.     (aref a (index+ i *word-0*)) (the card8 (ldb (byte 8 0) v)))
  655.   v)
  656.  
  657. (defun aref-card32 (a i)
  658.   (declare (type buffer-bytes a)
  659.        (type array-index i))
  660.   (declare (values card32))
  661.   #.(declare-buffun)
  662.   (the card32
  663.        (logior (the card32
  664.             (ash (the card8 (aref a (index+ i *long-3*))) 24))
  665.            (the card29
  666.             (ash (the card8 (aref a (index+ i *long-2*))) 16))
  667.            (the card16
  668.             (ash (the card8 (aref a (index+ i *long-1*))) 8))
  669.            (the card8
  670.             (aref a (index+ i *long-0*))))))
  671.  
  672. (defun aset-card32 (v a i)
  673.   (declare (type card32 v)
  674.        (type buffer-bytes a)
  675.        (type array-index i))
  676.   #.(declare-buffun)
  677.   (setf (aref a (index+ i *long-3*)) (the card8 (ldb (byte 8 24) v))
  678.     (aref a (index+ i *long-2*)) (the card8 (ldb (byte 8 16) v))
  679.     (aref a (index+ i *long-1*)) (the card8 (ldb (byte 8 8) v))
  680.     (aref a (index+ i *long-0*)) (the card8 (ldb (byte 8 0) v)))
  681.   v)
  682.  
  683. (defun aref-int32 (a i)
  684.   (declare (type buffer-bytes a)
  685.        (type array-index i))
  686.   (declare (values int32))
  687.   #.(declare-buffun)
  688.   (the int32
  689.        (logior (the int32
  690.             (ash (the int8 (aref-int8 a (index+ i *long-3*))) 24))
  691.            (the card29
  692.             (ash (the card8 (aref a (index+ i *long-2*))) 16))
  693.            (the card16
  694.             (ash (the card8 (aref a (index+ i *long-1*))) 8))
  695.            (the card8
  696.             (aref a (index+ i *long-0*))))))
  697.  
  698. (defun aset-int32 (v a i)
  699.   (declare (type int32 v)
  700.        (type buffer-bytes a)
  701.        (type array-index i))
  702.   #.(declare-buffun)
  703.   (setf (aref a (index+ i *long-3*)) (the card8 (ldb (byte 8 24) v))
  704.     (aref a (index+ i *long-2*)) (the card8 (ldb (byte 8 16) v))
  705.     (aref a (index+ i *long-1*)) (the card8 (ldb (byte 8 8) v))
  706.     (aref a (index+ i *long-0*)) (the card8 (ldb (byte 8 0) v)))
  707.   v)
  708.  
  709. (defun aref-card29 (a i)
  710.   (declare (type buffer-bytes a)
  711.        (type array-index i))
  712.   (declare (values card29))
  713.   #.(declare-buffun)
  714.   (the card29
  715.        (logior (the card29
  716.             (ash (the card8 (aref a (index+ i *long-3*))) 24))
  717.            (the card29
  718.             (ash (the card8 (aref a (index+ i *long-2*))) 16))
  719.            (the card16
  720.             (ash (the card8 (aref a (index+ i *long-1*))) 8))
  721.            (the card8
  722.             (aref a (index+ i *long-0*))))))
  723.  
  724. (defun aset-card29 (v a i)
  725.   (declare (type card29 v)
  726.        (type buffer-bytes a)
  727.        (type array-index i))
  728.   #.(declare-buffun)
  729.   (setf (aref a (index+ i *long-3*)) (the card8 (ldb (byte 8 24) v))
  730.     (aref a (index+ i *long-2*)) (the card8 (ldb (byte 8 16) v))
  731.     (aref a (index+ i *long-1*)) (the card8 (ldb (byte 8 8) v))
  732.     (aref a (index+ i *long-0*)) (the card8 (ldb (byte 8 0) v)))
  733.   v)
  734.  
  735. )
  736.  
  737. (defsetf aref-card8 (a i) (v)
  738.   `(aset-card8 ,v ,a ,i))
  739.  
  740. (defsetf aref-int8 (a i) (v)
  741.   `(aset-int8 ,v ,a ,i))
  742.  
  743. (defsetf aref-card16 (a i) (v)
  744.   `(aset-card16 ,v ,a ,i))
  745.  
  746. (defsetf aref-int16 (a i) (v)
  747.   `(aset-int16 ,v ,a ,i))
  748.  
  749. (defsetf aref-card32 (a i) (v)
  750.   `(aset-card32 ,v ,a ,i))
  751.  
  752. (defsetf aref-int32 (a i) (v)
  753.   `(aset-int32 ,v ,a ,i))
  754.  
  755. (defsetf aref-card29 (a i) (v)
  756.   `(aset-card29 ,v ,a ,i))
  757.  
  758. ;;; Other random conversions
  759.  
  760. (defun rgb-val->card16 (value)
  761.   ;; Short floats are good enough
  762.   (declare (type rgb-val value))
  763.   (declare (values card16))
  764.   #.(declare-buffun)
  765.   ;; Convert VALUE from float to card16
  766.   (the card16 (values (round (the rgb-val value) #.(/ 1.0s0 #xffff)))))
  767.  
  768. (defun card16->rgb-val (value) 
  769.   ;; Short floats are good enough
  770.   (declare (type card16 value))
  771.   (declare (values short-float))
  772.   #.(declare-buffun)
  773.   ;; Convert VALUE from card16 to float
  774.   (the short-float (* (the card16 value) #.(/ 1.0s0 #xffff))))
  775.  
  776. (defun radians->int16 (value)
  777.   ;; Short floats are good enough
  778.   (declare (type angle value))
  779.   (declare (values int16))
  780.   #.(declare-buffun)
  781.   (the int16 (values (round (the angle value) #.(float (/ pi 180.0s0 64.0s0) 0.0s0)))))
  782.  
  783. (defun int16->radians (value)
  784.   ;; Short floats are good enough
  785.   (declare (type int16 value))
  786.   (declare (values short-float))
  787.   #.(declare-buffun)
  788.   (the short-float (* (the int16 value) #.(coerce (/ pi 180.0 64.0) 'short-float))))
  789.  
  790.  
  791. ;;-----------------------------------------------------------------------------
  792. ;; Character transformation
  793. ;;-----------------------------------------------------------------------------
  794.  
  795.  
  796. ;;; This stuff transforms chars to ascii codes in card8's and back.
  797. ;;; You might have to hack it a little to get it to work for your machine.
  798.  
  799. (declaim (inline char->card8 card8->char))
  800.  
  801. (macrolet ((char-translators ()
  802.          (let ((alist
  803.              `(#-lispm
  804.                ;; The normal ascii codes for the control characters.
  805.                ,@`((#\Return . 13)
  806.                (#\Linefeed . 10)
  807.                (#\Rubout . 127)
  808.                (#\Page . 12)
  809.                (#\Tab . 9)
  810.                (#\Backspace . 8)
  811.                (#\Newline . 10)
  812.                (#\Space . 32))
  813.                ;; One the lispm, #\Newline is #\Return, but we'd really like
  814.                ;; #\Newline to translate to ascii code 10, so we swap the
  815.                ;; Ascii codes for #\Return and #\Linefeed. We also provide
  816.                ;; mappings from the counterparts of these control characters
  817.                ;; so that the character mapping from the lisp machine
  818.                ;; character set to ascii is invertible.
  819.                #+lispm
  820.                ,@`((#\Return . 10)   (,(code-char  10) . ,(char-code #\Return))
  821.                (#\Linefeed . 13) (,(code-char  13) . ,(char-code #\Linefeed))
  822.                (#\Rubout . 127)  (,(code-char 127) . ,(char-code #\Rubout))
  823.                (#\Page . 12)     (,(code-char  12) . ,(char-code #\Page))
  824.                (#\Tab . 9)       (,(code-char   9) . ,(char-code #\Tab))
  825.                (#\Backspace . 8) (,(code-char   8) . ,(char-code #\Backspace))
  826.                (#\Newline . 10)  (,(code-char  10) . ,(char-code #\Newline))
  827.                (#\Space . 32)    (,(code-char  32) . ,(char-code #\Space)))
  828.                ;; The rest of the common lisp charater set with the normal
  829.                ;; ascii codes for them.
  830.                (#\! . 33) (#\" . 34) (#\# . 35) (#\$ . 36)
  831.                (#\% . 37) (#\& . 38) (#\' . 39) (#\( . 40)
  832.                (#\) . 41) (#\* . 42) (#\+ . 43) (#\, . 44)
  833.                (#\- . 45) (#\. . 46) (#\/ . 47) (#\0 . 48)
  834.                (#\1 . 49) (#\2 . 50) (#\3 . 51) (#\4 . 52)
  835.                (#\5 . 53) (#\6 . 54) (#\7 . 55) (#\8 . 56)
  836.                (#\9 . 57) (#\: . 58) (#\; . 59) (#\< . 60)
  837.                (#\= . 61) (#\> . 62) (#\? . 63) (#\@ . 64)
  838.                (#\A . 65) (#\B . 66) (#\C . 67) (#\D . 68)
  839.                (#\E . 69) (#\F . 70) (#\G . 71) (#\H . 72)
  840.                (#\I . 73) (#\J . 74) (#\K . 75) (#\L . 76)
  841.                (#\M . 77) (#\N . 78) (#\O . 79) (#\P . 80)
  842.                (#\Q . 81) (#\R . 82) (#\S . 83) (#\T . 84)
  843.                (#\U . 85) (#\V . 86) (#\W . 87) (#\X . 88)
  844.                (#\Y . 89) (#\Z . 90) (#\[ . 91) (#\\ . 92)
  845.                (#\] . 93) (#\^ . 94) (#\_ . 95) (#\` . 96)
  846.                (#\a . 97) (#\b . 98) (#\c . 99) (#\d . 100)
  847.                (#\e . 101) (#\f . 102) (#\g . 103) (#\h . 104)
  848.                (#\i . 105) (#\j . 106) (#\k . 107) (#\l . 108)
  849.                (#\m . 109) (#\n . 110) (#\o . 111) (#\p . 112)
  850.                (#\q . 113) (#\r . 114) (#\s . 115) (#\t . 116)
  851.                (#\u . 117) (#\v . 118) (#\w . 119) (#\x . 120)
  852.                (#\y . 121) (#\z . 122) (#\{ . 123) (#\| . 124)
  853.                (#\} . 125) (#\~ . 126))))
  854.            (cond ((dolist (pair alist nil)
  855.             (when (not (= (char-code (car pair)) (cdr pair)))
  856.               (return t)))
  857.               `(progn
  858.              (defconstant *char-to-card8-translation-table*
  859.                       ',(let ((array (make-array
  860.                                (let ((max-char-code 255))
  861.                              (dolist (pair alist)
  862.                                (setq max-char-code
  863.                                  (max max-char-code
  864.                                       (char-code (car pair)))))
  865.                              (1+ max-char-code))
  866.                                :element-type 'card8)))
  867.                       (dotimes (i (length array))
  868.                         (setf (aref array i) (mod i 256)))
  869.                       (dolist (pair alist)
  870.                         (setf (aref array (char-code (car pair)))
  871.                           (cdr pair)))
  872.                       array))
  873.              (defconstant *card8-to-char-translation-table*
  874.                       ',(let ((array (make-string 256)))
  875.                       (dotimes (i (length array))
  876.                         (setf (aref array i) (code-char i)))
  877.                       (dolist (pair alist)
  878.                         (setf (aref array (cdr pair)) (car pair)))
  879.                       array))
  880.              #-Genera
  881.              (progn
  882.                  (defun char->card8 (char)
  883.                  (declare (type base-char char))
  884.                  #.(declare-buffun)
  885.                  (the card8 (aref (the (simple-array card8 (*))
  886.                            *char-to-card8-translation-table*)
  887.                           (the array-index (char-code char)))))
  888.                (defun card8->char (card8)
  889.                  (declare (type card8 card8))
  890.                  #.(declare-buffun)
  891.                  (the base-char
  892.                   (aref (the simple-string *card8-to-char-translation-table*)
  893.                     card8)))
  894.                )
  895.              #+Genera
  896.              (progn
  897.                (defun char->card8 (char)
  898.                  (declare lt:(side-effects reader reducible))
  899.                  (aref *char-to-card8-translation-table* (char-code char)))
  900.                (defun card8->char (card8)
  901.                  (declare lt:(side-effects reader reducible))
  902.                  (aref *card8-to-char-translation-table* card8))
  903.                )
  904.              (dotimes (i 256)
  905.                (unless (= i (char->card8 (card8->char i)))
  906.                  (warn "The card8->char mapping is not invertible through char->card8.  Info:~%~S"
  907.                    (list i
  908.                      (card8->char i)
  909.                      (char->card8 (card8->char i))))
  910.                  (return nil)))
  911.              (dotimes (i (length *char-to-card8-translation-table*))
  912.                (let ((char (code-char i)))
  913.                  (unless (eql char (card8->char (char->card8 char)))
  914.                    (warn "The char->card8 mapping is not invertible through card8->char.  Info:~%~S"
  915.                      (list char
  916.                        (char->card8 char)
  917.                        (card8->char (char->card8 char))))
  918.                    (return nil))))))
  919.              (t
  920.               `(progn
  921.              (defun char->card8 (char)
  922.                (declare (type base-char char))
  923.                #.(declare-buffun)
  924.                (the card8 (char-code char)))
  925.              (defun card8->char (card8)
  926.                (declare (type card8 card8))
  927.                #.(declare-buffun)
  928.                (the base-char (code-char card8)))
  929.              ))))))
  930.   (char-translators))
  931.  
  932. ;;-----------------------------------------------------------------------------
  933. ;; Process Locking
  934. ;;
  935. ;;    Common-Lisp doesn't provide process locking primitives, so we define
  936. ;;    our own here, based on Zetalisp primitives.  Holding-Lock is very
  937. ;;    similar to with-lock on The TI Explorer, and a little more efficient
  938. ;;    than with-process-lock on a Symbolics.
  939. ;;-----------------------------------------------------------------------------
  940.  
  941. ;;; MAKE-PROCESS-LOCK: Creating a process lock.
  942.  
  943. #-(or LispM excl Minima)
  944. (defun make-process-lock (name)
  945.   (declare (ignore name))
  946.   nil)
  947.  
  948. #+excl
  949. (defun make-process-lock (name)
  950.   (mp:make-process-lock :name name))
  951.  
  952. #+(and LispM (not Genera))
  953. (defun make-process-lock (name)
  954.   (vector nil name))
  955.  
  956. #+Genera
  957. (defun make-process-lock (name)
  958.   (process:make-lock name :flavor 'clx-lock))
  959.  
  960. #+Minima
  961. (defun make-process-lock (name)
  962.   (minima:make-lock name))
  963.  
  964. ;;; HOLDING-LOCK: Execute a body of code with a lock held.
  965.  
  966. ;;; The holding-lock macro takes a timeout keyword argument.  EVENT-LISTEN
  967. ;;; passes its timeout to the holding-lock macro, so any timeout you want to
  968. ;;; work for event-listen you should do for holding-lock.
  969.  
  970. ;; If you're not sharing DISPLAY objects within a multi-processing
  971. ;; shared-memory environment, this is sufficient
  972. #-(or lispm excl lcl3.0 Minima CMU)
  973. (defmacro holding-lock ((locator display &optional whostate &key timeout) &body body)
  974.   (declare (ignore locator display whostate timeout))
  975.   `(progn ,@body))
  976.  
  977. #+Genera
  978. (defmacro holding-lock ((locator display &optional whostate &key timeout)
  979.             &body body)
  980.   (declare (ignore whostate))
  981.   `(process:with-lock (,locator :timeout ,timeout)
  982.      (let ((.debug-io. (buffer-debug-io ,display)))
  983.        (scl:let-if .debug-io. ((*debug-io* .debug-io.))
  984.      ,@body))))
  985.  
  986. #+(and lispm (not Genera))
  987. (defmacro holding-lock ((locator display &optional whostate &key timeout)
  988.             &body body)
  989.   (declare (ignore display))
  990.   ;; This macro is for use in a multi-process environment.
  991.   (let ((lock (gensym))
  992.     (have-lock (gensym))
  993.     (timeo (gensym)))
  994.     `(let* ((,lock (zl:locf (svref ,locator 0)))
  995.         (,have-lock (eq (car ,lock) sys:current-process))
  996.         (,timeo ,timeout))
  997.        (unwind-protect 
  998.        (when (cond (,have-lock)
  999.                ((#+explorer si:%store-conditional
  1000.              #-explorer sys:store-conditional
  1001.              ,lock nil sys:current-process))
  1002.                ((null ,timeo)
  1003.             (sys:process-lock ,lock nil ,(or whostate "CLX Lock")))
  1004.                ((sys:process-wait-with-timeout
  1005.                 ,(or whostate "CLX Lock") (round (* ,timeo 60.))
  1006.               #'(lambda (lock process)
  1007.                   (#+explorer si:%store-conditional
  1008.                    #-explorer sys:store-conditional
  1009.                    lock nil process))
  1010.               ,lock sys:current-process)))
  1011.          ,@body)
  1012.      (unless ,have-lock
  1013.        (#+explorer si:%store-conditional
  1014.         #-explorer sys:store-conditional
  1015.         ,lock sys:current-process nil))))))
  1016.  
  1017. ;; Lucid has a process locking mechanism as well under release 3.0
  1018. #+lcl3.0
  1019. (defmacro holding-lock ((locator display &optional whostate &key timeout)
  1020.             &body body)
  1021.   (declare (ignore display))
  1022.   (if timeout
  1023.       ;; Hair to support timeout.
  1024.       `(let ((.have-lock. (eq ,locator lcl:*current-process*))
  1025.          (.timeout. ,timeout))
  1026.      (unwind-protect
  1027.          (when (cond (.have-lock.)
  1028.              ((conditional-store ,locator nil lcl:*current-process*))
  1029.              ((null .timeout.)
  1030.               (lcl:process-lock ,locator)
  1031.               t)
  1032.              ((lcl:process-wait-with-timeout ,whostate .timeout.
  1033.                 #'(lambda ()
  1034.                 (conditional-store ,locator nil lcl:*current-process*))))
  1035.              ;; abort the PROCESS-UNLOCK if actually timing out
  1036.              (t
  1037.               (setf .have-lock. :abort)
  1038.               nil))
  1039.            ,@body)
  1040.        (unless .have-lock. 
  1041.          (lcl:process-unlock ,locator))))
  1042.     `(lcl:with-process-lock (,locator)
  1043.        ,@body)))
  1044.  
  1045.  
  1046. #+excl
  1047. (defmacro holding-lock ((locator display &optional whostate &key timeout)
  1048.             &body body)
  1049.   (declare (ignore display))
  1050.   `(let (.hl-lock. .hl-obtained-lock. .hl-curproc.)
  1051.      (unwind-protect
  1052.      (block .hl-doit.
  1053.        (when mp::*scheduler-stack-group* ; fast test for scheduler running
  1054.          (setq .hl-lock. ,locator
  1055.            .hl-curproc. mp::*current-process*)
  1056.          (when (and .hl-curproc.    ; nil if in process-wait fun
  1057.             (not (eq (mp::process-lock-locker .hl-lock.)
  1058.                  .hl-curproc.)))
  1059.            ;; Then we need to grab the lock.
  1060.            ,(if timeout
  1061.             `(if (not (mp::process-lock .hl-lock. .hl-curproc.
  1062.                         ,whostate ,timeout))
  1063.              (return-from .hl-doit. nil))
  1064.           `(mp::process-lock .hl-lock. .hl-curproc.
  1065.                      ,@(when whostate `(,whostate))))
  1066.            ;; There is an apparent race condition here.  However, there is
  1067.            ;; no actual race condition -- our implementation of mp:process-
  1068.            ;; lock guarantees that the lock will still be held when it
  1069.            ;; returns, and no interrupt can happen between that and the
  1070.            ;; execution of the next form.  -- jdi 2/27/91
  1071.            (setq .hl-obtained-lock. t)))
  1072.        ,@body)
  1073.        (if (and .hl-obtained-lock.
  1074.         ;; Note -- next form added to allow error handler inside
  1075.         ;; body to unlock the lock prematurely if it knows that
  1076.         ;; the current process cannot possibly continue but will
  1077.         ;; throw out (or is it throw up?).
  1078.         (eq (mp::process-lock-locker .hl-lock.) .hl-curproc.))
  1079.        (mp::process-unlock .hl-lock. .hl-curproc.)))))
  1080.  
  1081. #+Minima
  1082. (defmacro holding-lock ((locator display &optional whostate &key timeout) &body body)
  1083.   `(holding-lock-1 #'(lambda () ,@body) ,locator ,display
  1084.            ,@(and whostate `(:whostate ,whostate))
  1085.            ,@(and timeout `(:timeout ,timeout))))
  1086.  
  1087. #+Minima
  1088. (defun holding-lock-1 (continuation lock display &key (whostate "Lock") timeout)
  1089.   (declare (dynamic-extent continuation))
  1090.   (declare (ignore display whostate timeout))
  1091.   (minima:with-lock (lock)
  1092.     (funcall continuation)))
  1093.  
  1094. ;;; HOLDING-LOCK for CMU Common Lisp.
  1095. ;;;
  1096. ;;; We are not multi-processing, but we use this macro to try to protect
  1097. ;;; against re-entering request functions.  This can happen if an interrupt
  1098. ;;; occurs and the handler attempts to use X over the same display connection.
  1099. ;;; This can happen if the GC hooks are used to notify the user over the same
  1100. ;;; display connection.  We lock out GC's just as a dummy check for our users.
  1101. ;;; Locking out interrupts has the problem that CLX always waits for replies
  1102. ;;; within this dynamic scope, so if the server cannot reply for some reason,
  1103. ;;; we potentially dead-lock without interrupts.
  1104. ;;;
  1105. #+CMU
  1106. (defmacro holding-lock ((locator display &optional whostate &key timeout)
  1107.             &body body)
  1108.   (declare (ignore locator display whostate timeout))
  1109.   `(lisp::without-gcing (system:without-interrupts (progn ,@body))))
  1110.  
  1111.  
  1112. ;;; WITHOUT-ABORTS
  1113.  
  1114. ;;; If you can inhibit asynchronous keyboard aborts inside the body of this
  1115. ;;; macro, then it is a good idea to do this.  This macro is wrapped around
  1116. ;;; request writing and reply reading to ensure that requests are atomically
  1117. ;;; written and replies are atomically read from the stream.
  1118.  
  1119. #-(or Genera excl lcl3.0)
  1120. (defmacro without-aborts (&body body)
  1121.   `(progn ,@body))
  1122.  
  1123. #+Genera
  1124. (defmacro without-aborts (&body body)
  1125.   `(sys:without-aborts (clx "CLX is in the middle of an operation that should be atomic.")
  1126.      ,@body))
  1127.  
  1128. #+excl
  1129. (defmacro without-aborts (&body body)
  1130.   `(without-interrupts ,@body))
  1131.     
  1132. #+lcl3.0
  1133. (defmacro without-aborts (&body body)
  1134.   `(lcl:with-interruptions-inhibited ,@body))
  1135.  
  1136. ;;; PROCESS-BLOCK: Wait until a given predicate returns a non-NIL value.
  1137. ;;; Caller guarantees that PROCESS-WAKEUP will be called after the predicate's
  1138. ;;; value changes.
  1139.  
  1140. #-(or lispm excl lcl3.0 Minima)
  1141. (defun process-block (whostate predicate &rest predicate-args)
  1142.   (declare (ignore whostate))
  1143.   (or (apply predicate predicate-args)
  1144.       (error "Program tried to wait with no scheduler.")))
  1145.  
  1146. #+Genera
  1147. (defun process-block (whostate predicate &rest predicate-args)
  1148.   (declare (type function predicate)
  1149.        #+clx-ansi-common-lisp
  1150.        (dynamic-extent predicate)
  1151.        #-clx-ansi-common-lisp
  1152.        (sys:downward-funarg predicate))
  1153.   (apply #'process:block-process whostate predicate predicate-args))
  1154.  
  1155. #+(and lispm (not Genera))
  1156. (defun process-block (whostate predicate &rest predicate-args)
  1157.   (declare (type function predicate)
  1158.        #+clx-ansi-common-lisp
  1159.        (dynamic-extent predicate)
  1160.        #-clx-ansi-common-lisp 
  1161.        (sys:downward-funarg predicate))
  1162.   (apply #'global:process-wait whostate predicate predicate-args))
  1163.  
  1164. #+excl
  1165. (defun process-block (whostate predicate &rest predicate-args)
  1166.   (if mp::*scheduler-stack-group*
  1167.       (apply #'mp::process-wait whostate predicate predicate-args)
  1168.       (or (apply predicate predicate-args)
  1169.       (error "Program tried to wait with no scheduler."))))
  1170.  
  1171. #+lcl3.0
  1172. (defun process-block (whostate predicate &rest predicate-args)
  1173.   (declare (dynamic-extent predicate-args))
  1174.   (apply #'lcl:process-wait whostate predicate predicate-args))
  1175.  
  1176. #+Minima
  1177. (defun process-block (whostate predicate &rest predicate-args)
  1178.   (declare (type function predicate)
  1179.        (dynamic-extent predicate))
  1180.   (apply #'minima:process-wait whostate predicate predicate-args))
  1181.  
  1182. ;;; PROCESS-WAKEUP: Check some other process' wait function.
  1183.  
  1184. (declaim (inline process-wakeup))
  1185.  
  1186. #-(or excl Genera Minima)
  1187. (defun process-wakeup (process)
  1188.   (declare (ignore process))
  1189.   nil)
  1190.  
  1191. #+excl
  1192. (defun process-wakeup (process)
  1193.   (let ((curproc mp::*current-process*))
  1194.     (when (and curproc process)
  1195.       (unless (mp::process-p curproc)
  1196.     (error "~s is not a process" curproc))
  1197.       (unless (mp::process-p process)
  1198.     (error "~s is not a process" process))
  1199.       (if (> (mp::process-priority process) (mp::process-priority curproc))
  1200.       (mp::process-allow-schedule process)))))
  1201.  
  1202. #+Genera
  1203. (defun process-wakeup (process)
  1204.   (process:wakeup process))
  1205.  
  1206. #+Minima
  1207. (defun process-wakeup (process)
  1208.   (minima:process-wakeup process))
  1209.  
  1210. ;;; CURRENT-PROCESS: Return the current process object for input locking and
  1211. ;;; for calling PROCESS-WAKEUP.
  1212.  
  1213. (declaim (inline current-process))
  1214.  
  1215. ;;; Default return NIL, which is acceptable even if there is a scheduler.
  1216.  
  1217. #-(or lispm excl lcl3.0 Minima)
  1218. (defun current-process ()
  1219.   nil)
  1220.  
  1221. #+lispm
  1222. (defun current-process ()
  1223.   sys:current-process)
  1224.  
  1225. #+excl
  1226. (defun current-process ()
  1227.   (and mp::*scheduler-stack-group*
  1228.        mp::*current-process*))
  1229.  
  1230. #+lcl3.0
  1231. (defun current-process ()
  1232.   lcl:*current-process*)
  1233.  
  1234. #+Minima
  1235. (defun current-process ()
  1236.   (minima:current-process))
  1237.  
  1238. ;;; WITHOUT-INTERRUPTS -- provide for atomic operations.
  1239.  
  1240. #-(or lispm excl lcl3.0 Minima)
  1241. (defmacro without-interrupts (&body body)
  1242.   `(progn ,@body))
  1243.  
  1244. #+(and lispm (not Genera))
  1245. (defmacro without-interrupts (&body body)
  1246.   `(sys:without-interrupts ,@body))
  1247.  
  1248. #+Genera
  1249. (defmacro without-interrupts (&body body)
  1250.   `(process:with-no-other-processes ,@body))
  1251.  
  1252. #+LCL3.0
  1253. (defmacro without-interrupts (&body body)
  1254.   `(lcl:with-scheduling-inhibited ,@body))
  1255.  
  1256. #+Minima
  1257. (defmacro without-interrupts (&body body)
  1258.   `(minima:with-no-other-processes ,@body))
  1259.  
  1260. ;;; CONDITIONAL-STORE:
  1261.  
  1262. ;; This should use GET-SETF-METHOD to avoid evaluating subforms multiple times.
  1263. ;; It doesn't because CLtL doesn't pass the environment to GET-SETF-METHOD.
  1264. #-CLISP
  1265. (defmacro conditional-store (place old-value new-value)
  1266.   `(without-interrupts
  1267.      (cond ((eq ,place ,old-value)
  1268.         (setf ,place ,new-value)
  1269.         t))))
  1270. #+CLISP
  1271. (defmacro conditional-store (place old-value new-value &environment env)
  1272.   (multiple-value-bind (SM1 SM2 SM3 SM4 SM5) (get-setf-method place env)
  1273.     `(without-interrupts
  1274.        (let* ,(mapcar #'list SM1 SM2)
  1275.          (cond ((eq ,SM5 ,old-value)
  1276.                 (let ((,(first SM3) ,new-value)) ,SM4)
  1277.                 t
  1278. ) )  ) ) )     )
  1279.  
  1280. ;;;----------------------------------------------------------------------------
  1281. ;;; IO Error Recovery
  1282. ;;;    All I/O operations are done within a WRAP-BUF-OUTPUT macro.
  1283. ;;;    It prevents multiple mindless errors when the network craters.
  1284. ;;;
  1285. ;;;----------------------------------------------------------------------------
  1286.  
  1287. #-Genera
  1288. (defmacro wrap-buf-output ((buffer) &body body)
  1289.   ;; Error recovery wrapper
  1290.   `(unless (buffer-dead ,buffer)
  1291.      ,@body))
  1292.  
  1293. #+Genera
  1294. (defmacro wrap-buf-output ((buffer) &body body)
  1295.   ;; Error recovery wrapper
  1296.   `(let ((.buffer. ,buffer))
  1297.      (unless (buffer-dead .buffer.)
  1298.        (scl:condition-bind
  1299.      (((sys:network-error)
  1300.        #'(lambda (error)
  1301.            (scl:condition-case () 
  1302.             (funcall (buffer-close-function .buffer.) .buffer. :abort t)
  1303.           (sys:network-error))
  1304.            (setf (buffer-dead .buffer.) error)
  1305.            (setf (buffer-output-stream .buffer.) nil)
  1306.            (setf (buffer-input-stream .buffer.) nil)
  1307.            nil)))
  1308.      ,@body))))
  1309.  
  1310. #-Genera
  1311. (defmacro wrap-buf-input ((buffer) &body body)
  1312.   (declare (ignore buffer))
  1313.   ;; Error recovery wrapper
  1314.   `(progn ,@body))
  1315.  
  1316. #+Genera
  1317. (defmacro wrap-buf-input ((buffer) &body body)
  1318.   ;; Error recovery wrapper
  1319.   `(let ((.buffer. ,buffer))
  1320.      (scl:condition-bind
  1321.        (((sys:network-error)
  1322.      #'(lambda (error)
  1323.          (scl:condition-case () 
  1324.           (funcall (buffer-close-function .buffer.) .buffer. :abort t)
  1325.         (sys:network-error))
  1326.          (setf (buffer-dead .buffer.) error)
  1327.          (setf (buffer-output-stream .buffer.) nil)
  1328.          (setf (buffer-input-stream .buffer.) nil)
  1329.          nil)))
  1330.        ,@body)))
  1331.  
  1332.  
  1333. ;;;----------------------------------------------------------------------------
  1334. ;;; System dependent IO primitives
  1335. ;;;    Functions for opening, reading writing forcing-output and closing 
  1336. ;;;    the stream to the server.
  1337. ;;;----------------------------------------------------------------------------
  1338.  
  1339. ;;; OPEN-X-STREAM - create a stream for communicating to the appropriate X
  1340. ;;; server
  1341.  
  1342. #-(or explorer Genera lucid kcl ibcl excl Minima CMU)
  1343. (defun open-x-stream (host display protocol)
  1344.   host display protocol ;; unused
  1345.   (error "OPEN-X-STREAM not implemented yet."))
  1346.  
  1347. ;;; Genera:
  1348.  
  1349. ;;; TCP and DNA are both layered products, so try to work with either one.
  1350.  
  1351. #+Genera
  1352. (when (fboundp 'tcp:add-tcp-port-for-protocol)
  1353.   (tcp:add-tcp-port-for-protocol :x-window-system 6000))
  1354.  
  1355. #+Genera
  1356. (when (fboundp 'dna:add-dna-contact-id-for-protocol)
  1357.   (dna:add-dna-contact-id-for-protocol :x-window-system "X$X0"))
  1358.  
  1359. #+Genera
  1360. (net:define-protocol :x-window-system (:x-window-system :byte-stream)
  1361.   (:invoke-with-stream ((stream :characters nil :ascii-translation nil))
  1362.     stream))
  1363.  
  1364. #+Genera
  1365. (eval-when (compile)
  1366.   (compiler:function-defined 'tcp:open-tcp-stream)
  1367.   (compiler:function-defined 'dna:open-dna-bidirectional-stream))
  1368.  
  1369. #+Genera
  1370. (defun open-x-stream (host display protocol)
  1371.   (let ((host (net:parse-host host)))
  1372.     (if (or protocol (plusp display))
  1373.     ;; The protocol was specified or the display isn't 0, so we
  1374.     ;; can't use the Generic Network System.  If the protocol was
  1375.     ;; specified, then use that protocol, otherwise, blindly use
  1376.     ;; TCP.
  1377.     (ccase protocol
  1378.       ((:tcp nil)
  1379.        (tcp:open-tcp-stream
  1380.          host (+ *x-tcp-port* display) nil
  1381.          :direction :io
  1382.          :characters nil
  1383.          :ascii-translation nil))
  1384.       ((:dna)
  1385.        (dna:open-dna-bidirectional-stream
  1386.          host (format nil "X$X~D" display)
  1387.          :characters nil
  1388.          :ascii-translation nil)))
  1389.       (let ((neti:*invoke-service-automatic-retry* t))
  1390.     (net:invoke-service-on-host :x-window-system host)))))
  1391.  
  1392. #+explorer
  1393. (defun open-x-stream (host display protocol)
  1394.   (declare (ignore protocol))
  1395.   (net:open-connection-on-medium
  1396.     (net:parse-host host)            ;Host
  1397.     :byte-stream                ;Medium
  1398.     "X11"                    ;Logical contact name
  1399.     :stream-type :character-stream
  1400.     :direction :bidirectional
  1401.     :timeout-after-open nil
  1402.     :remote-port (+ *x-tcp-port* display)))
  1403.  
  1404. #+explorer
  1405. (net:define-logical-contact-name
  1406.   "X11"
  1407.   `((:local "X11")
  1408.     (:chaos "X11")
  1409.     (:nsp-stream "X11")
  1410.     (:tcp ,*x-tcp-port*)))
  1411.  
  1412. #+lucid
  1413. (defun open-x-stream (host display protocol)
  1414.   protocol ;; unused
  1415.   (let ((fd (connect-to-server host display)))
  1416.     (when (minusp fd)
  1417.       (error "Failed to connect to server: ~A ~D" host display))
  1418.     (user::make-lisp-stream :input-handle fd
  1419.                 :output-handle fd
  1420.                 :element-type 'unsigned-byte
  1421.                 #-lcl3.0 :stream-type #-lcl3.0 :ephemeral)))
  1422.  
  1423. #+(or kcl ibcl)
  1424. (defun open-x-stream (host display protocol)
  1425.   protocol ;; unused
  1426.   (let ((stream (open-socket-stream host display)))
  1427.     (if (streamp stream)
  1428.     stream
  1429.       (error "Cannot connect to server: ~A:~D" host display))))
  1430.  
  1431. #+CLISP
  1432. (defun open-x-stream (host display protocol)
  1433.   protocol ;; unused
  1434.   (let ((stream (system::make-socket-stream host display)))
  1435.     (if (streamp stream)
  1436.       stream
  1437.       (error "Cannot connect to server: ~A:~D" host display))))
  1438.  
  1439. #+excl
  1440. ;;
  1441. ;; Note that since we don't use the CL i/o facilities to do i/o, the display
  1442. ;; input and output "stream" is really a file descriptor (fixnum).
  1443. ;;
  1444. (defun open-x-stream (host display protocol)
  1445.   (declare (ignore protocol));; unused
  1446.   (let ((fd (connect-to-server (string host) display)))
  1447.     (when (minusp fd)
  1448.       (error "Failed to connect to server: ~A ~D" host display))
  1449.     fd))
  1450.  
  1451. #+Minima
  1452. (defun open-x-stream (host display protocol)
  1453.   (declare (ignore protocol));; unused
  1454.   (minima:open-tcp-stream (minima:gensym-tcp-port)
  1455.               (apply #'minima:make-ip-address (cdr (host-address host)))
  1456.               (+ *x-tcp-port* display) :element-type '(unsigned-byte 8)))
  1457.  
  1458. ;;; OPEN-X-STREAM -- for CMU Common Lisp.
  1459. ;;;
  1460. ;;; The file descriptor here just gets tossed into the stream slot of the
  1461. ;;; display object instead of a stream.
  1462. ;;;
  1463. #+CMU
  1464. (defun open-x-stream (host display protocol)
  1465.   (declare (ignore protocol))
  1466.   (let ((server-fd (connect-to-server host display)))
  1467.     (unless (plusp server-fd)
  1468.       (error "Failed to connect to X11 server: ~A (display ~D)" host display))
  1469.     (system:make-fd-stream server-fd :input t :output t
  1470.                :element-type '(unsigned-byte 8))))
  1471.  
  1472. ;;; BUFFER-READ-DEFAULT - read data from the X stream
  1473.  
  1474. #+(or Genera explorer)
  1475. (defun buffer-read-default (display vector start end timeout)
  1476.   ;; returns non-NIL if EOF encountered
  1477.   ;; Returns :TIMEOUT when timeout exceeded
  1478.   (declare (type display display)
  1479.        (type buffer-bytes vector)
  1480.        (type array-index start end)
  1481.        (type (or null number) timeout))
  1482.   #.(declare-buffun)
  1483.   (let ((stream (display-input-stream display)))
  1484.     (or (cond ((null stream))
  1485.           ((funcall stream :listen) nil)
  1486.           ((eql timeout 0) :timeout)
  1487.           ((buffer-input-wait-default display timeout)))
  1488.     (multiple-value-bind (ignore eofp)
  1489.         (funcall stream :string-in nil vector start end)
  1490.       eofp))))
  1491.  
  1492.  
  1493. #+excl
  1494. ;;
  1495. ;; Rewritten 10/89 to not use foreign function interface to do I/O.
  1496. ;;
  1497. (defun buffer-read-default (display vector start end timeout)
  1498.   (declare (type display display)
  1499.        (type buffer-bytes vector)
  1500.        (type array-index start end)
  1501.        (type (or null number) timeout))
  1502.   #.(declare-buffun)
  1503.     
  1504.   (let* ((howmany (- end start))
  1505.      (fd (display-input-stream display)))
  1506.     (declare (type array-index howmany)
  1507.          (fixnum fd))
  1508.       
  1509.     (or (cond ((fd-char-avail-p fd) nil)
  1510.           ((eql timeout 0) :timeout)
  1511.           ((buffer-input-wait-default display timeout)))
  1512.     (fd-read-bytes fd vector start howmany))))
  1513.  
  1514.  
  1515. #+lcl3.0
  1516. (defmacro with-underlying-stream ((variable stream display direction) &body body)
  1517.   `(let ((,variable
  1518.       (or (getf (display-plist ,display) ',direction)
  1519.           (setf (getf (display-plist ,display) ',direction)
  1520.             (lucid::underlying-stream
  1521.               ,stream ,(if (eq direction 'input) :input :output))))))
  1522.      ,@body))
  1523.  
  1524. #+lcl3.0
  1525. (defun buffer-read-default (display vector start end timeout)
  1526.   ;;Note that LISTEN must still be done on "slow stream" or the I/O system
  1527.   ;;gets confused.  But reading should be done from "fast stream" for speed.
  1528.   ;;We used to inhibit scheduling because there were races in Lucid's
  1529.   ;;multitasking system.  Empirical evidence suggests they may be gone now.
  1530.   ;;Should you decide you need to inhibit scheduling, do it around the
  1531.   ;;lcl:read-array.
  1532.   (declare (type display display)
  1533.        (type buffer-bytes vector)
  1534.        (type array-index start end)
  1535.        (type (or null number) timeout))
  1536.   #.(declare-buffun)
  1537.   (let ((stream (display-input-stream display)))
  1538.     (declare (type (or null stream) stream))
  1539.     (or (cond ((null stream))
  1540.           ((listen stream) nil)
  1541.           ((eql timeout 0) :timeout)
  1542.           ((buffer-input-wait-default display timeout)))
  1543.     (with-underlying-stream (stream stream display input)
  1544.       (eq (lcl:read-array stream vector start end nil :eof) :eof)))))
  1545.  
  1546. #+Minima
  1547. (defun buffer-read-default (display vector start end timeout)
  1548.   ;; returns non-NIL if EOF encountered
  1549.   ;; Returns :TIMEOUT when timeout exceeded
  1550.   (declare (type display display)
  1551.        (type buffer-bytes vector)
  1552.        (type array-index start end)
  1553.        (type (or null number) timeout))
  1554.   #.(declare-buffun)
  1555.   (let ((stream (display-input-stream display)))
  1556.     (or (cond ((null stream))
  1557.           ((listen stream) nil)
  1558.           ((eql timeout 0) :timeout)
  1559.           ((buffer-input-wait-default display timeout)))
  1560.     (loop while (< start end) do
  1561.       (multiple-value-bind (buffer bstart bend)
  1562.           (minima:get-input-buffer stream nil)
  1563.         (when (null buffer) (return t))
  1564.         (let ((n (min (- end start) (- bend bstart))))
  1565.           (replace vector buffer
  1566.                :start1 start :end1 (incf start n)
  1567.                :start2 bstart :end2 (incf bstart n)))
  1568.         (minima:advance-input-buffer stream bstart)))
  1569.     nil)))
  1570.  
  1571. ;;; BUFFER-READ-DEFAULT for CMU Common Lisp.
  1572. ;;;
  1573. ;;;    If timeout is 0, then we call LISTEN to see if there is any input.
  1574. ;;; Timeout 0 is the only case where READ-INPUT dives into BUFFER-READ without
  1575. ;;; first calling BUFFER-INPUT-WAIT-DEFAULT.
  1576. ;;;
  1577. #+CMU
  1578. (defun buffer-read-default (display vector start end timeout)
  1579.   (declare (type display display)
  1580.        (type buffer-bytes vector)
  1581.        (type array-index start end)
  1582.        (type (or null fixnum) timeout))
  1583.   #.(declare-buffun)
  1584.   (cond ((and (eql timeout 0)
  1585.           (not (listen (display-input-stream display))))
  1586.      :timeout)
  1587.     (t
  1588.      (system:read-n-bytes (display-input-stream display)
  1589.                   vector start (- end start))
  1590.      nil)))
  1591.  
  1592. #+CLISP
  1593. (defun buffer-read-default (display vector start end timeout)
  1594.   (declare (type display display)
  1595.          (type buffer-bytes vector)
  1596.          (type array-index start end)
  1597.          (type (or null fixnum) timeout))
  1598.   #.(declare-buffun)
  1599.   (let ((stream (display-input-stream display)))
  1600.     (cond ((and (eql timeout 0) (not (listen stream))) :timeout)
  1601.           (t (system::read-n-bytes stream vector start (- end start)) nil)
  1602. ) ) )
  1603.  
  1604. #+akcl
  1605. (defun buffer-read-default (display vector start end timeout)
  1606.   (declare (type display display)
  1607.        (type buffer-bytes vector)
  1608.        (type array-index start end)
  1609.        (type (or null (rational 0 *) (float 0.0 *)) timeout))
  1610. ; (if *debug-read* (format t "~%doing buffer-read-default ~a(~a)(~a) " display start timeout))
  1611.   (let ((stream (display-input-stream display)))
  1612.     (declare (type (or null stream) stream))
  1613.     (let ((tem (and stream (si::fp-input-stream stream))))
  1614.       (if tem (setq stream tem)))
  1615.     (or (cond ((null stream))
  1616.           ((listen stream) nil)
  1617.           ((eql timeout 0) :timeout)
  1618.           ((buffer-input-wait-default display timeout)))
  1619.     (do* ((index start (index1+ index)))
  1620.          ((index>= index end) nil)
  1621.       (declare (type array-index index))
  1622.       (let ((c (read-byte stream nil -1)))
  1623.         (declare (type fixnum c))
  1624. ;        (if *debug-read* (format t "(~d)" c))
  1625.         (if (eql c -1)
  1626.         (return t)
  1627.           (setf (aref vector index) (the card8 c))))))))
  1628.  
  1629. ;; WARNING:
  1630. ;;;    CLX performance will suffer if your lisp uses read-byte for
  1631. ;;;    receiving all data from the X Window System server.
  1632. ;;;    You are encouraged to write a specialized version of
  1633. ;;;    buffer-read-default that does block transfers.
  1634. #-(or Genera explorer excl lcl3.0 Minima CMU CLISP akcl)
  1635. (defun buffer-read-default (display vector start end timeout)
  1636.   (declare (type display display)
  1637.        (type buffer-bytes vector)
  1638.        (type array-index start end)
  1639.        (type (or null (rational 0 *) (float 0.0 *)) timeout))
  1640.   #.(declare-buffun)
  1641.   (let ((stream (display-input-stream display)))
  1642.     (declare (type (or null stream) stream))
  1643.     (or (cond ((null stream))
  1644.           ((listen stream) nil)
  1645.           ((eql timeout 0) :timeout)
  1646.           ((buffer-input-wait-default display timeout)))
  1647.     (do* ((index start (index1+ index)))
  1648.          ((index>= index end) nil)
  1649.       (declare (type array-index index))
  1650.       (let ((c (read-byte stream nil nil)))
  1651.         (declare (type (or null card8) c))
  1652.         (if (null c)
  1653.         (return t)
  1654.           (setf (aref vector index) (the card8 c))))))))
  1655.  
  1656. ;;; BUFFER-WRITE-DEFAULT - write data to the X stream
  1657.  
  1658. #+(or Genera explorer)
  1659. (defun buffer-write-default (vector display start end)
  1660.   ;; The default buffer write function for use with common-lisp streams
  1661.   (declare (type buffer-bytes vector)
  1662.        (type display display)
  1663.        (type array-index start end))
  1664.   #.(declare-buffun)
  1665.   (let ((stream (display-output-stream display)))
  1666.     (declare (type (or null stream) stream))
  1667.     (unless (null stream) 
  1668.       (write-string vector stream :start start :end end))))
  1669.  
  1670. #+excl
  1671. (defun buffer-write-default (vector display start end)
  1672.   (declare (type buffer-bytes vector)
  1673.        (type display display)
  1674.        (type array-index start end))
  1675.   #.(declare-buffun)
  1676.   (excl::filesys-write-bytes (display-output-stream display) vector start
  1677.                  (- end start)))
  1678.   
  1679. #+lcl3.0
  1680. (defun buffer-write-default (vector display start end)
  1681.   ;;We used to inhibit scheduling because there were races in Lucid's
  1682.   ;;multitasking system.  Empirical evidence suggests they may be gone now.
  1683.   ;;Should you decide you need to inhibit scheduling, do it around the
  1684.   ;;lcl:write-array.
  1685.   (declare (type display display)
  1686.        (type buffer-bytes vector)
  1687.        (type array-index start end))
  1688.   #.(declare-buffun)
  1689.   (let ((stream (display-output-stream display)))
  1690.     (declare (type (or null stream) stream))
  1691.     (unless (null stream) 
  1692.       (with-underlying-stream (stream stream display output)
  1693.     (lcl:write-array stream vector start end)))))
  1694.  
  1695. #+Minima
  1696. (defun buffer-write-default (vector display start end)
  1697.   ;; The default buffer write function for use with common-lisp streams
  1698.   (declare (type buffer-bytes vector)
  1699.        (type display display)
  1700.        (type array-index start end))
  1701.   #.(declare-buffun)
  1702.   (let ((stream (display-output-stream display)))
  1703.     (declare (type (or null stream) stream))
  1704.     (unless (null stream) 
  1705.       (loop while (< start end) do
  1706.     (multiple-value-bind (buffer bstart bend)
  1707.         (minima:get-output-buffer stream)
  1708.       (let ((n (min (- end start) (- bend bstart))))
  1709.         (replace buffer vector
  1710.              :start1 bstart :end1 (incf bstart n) :start2 start :end2 (incf start n) ))
  1711.         (minima:advance-output-buffer stream bstart))))))
  1712.  
  1713. #+CMU
  1714. (defun buffer-write-default (vector display start end)
  1715.   (declare (type buffer-bytes vector)
  1716.        (type display display)
  1717.        (type array-index start end))
  1718.   #.(declare-buffun)
  1719.   (system:output-raw-bytes (display-output-stream display) vector start end)
  1720.   nil)
  1721.  
  1722. #+CLISP
  1723. (defun buffer-write-default (vector display start end)
  1724.   (declare (type buffer-bytes vector)
  1725.          (type display display)
  1726.          (type array-index start end))
  1727.   #.(declare-buffun)
  1728.   (system::write-n-bytes (display-output-stream display) vector start (- end start))
  1729.   nil
  1730. )
  1731.  
  1732. ;;; WARNING:
  1733. ;;;    CLX performance will be severely degraded if your lisp uses
  1734. ;;;    write-byte to send all data to the X Window System server.
  1735. ;;;    You are STRONGLY encouraged to write a specialized version
  1736. ;;;    of buffer-write-default that does block transfers.
  1737.  
  1738. #-(or Genera explorer excl lcl3.0 Minima CMU CLISP)
  1739. (defun buffer-write-default (vector display start end)
  1740.   ;; The default buffer write function for use with common-lisp streams
  1741.   (declare (type buffer-bytes vector)
  1742.        (type display display)
  1743.        (type array-index start end))
  1744.   #.(declare-buffun)
  1745.   (let ((stream (display-output-stream display)))
  1746.     (declare (type (or null stream) stream))
  1747.     (unless (null stream)
  1748.       (with-vector (vector buffer-bytes)
  1749.     (do ((index start (index1+ index)))
  1750.         ((index>= index end))
  1751.       (declare (type array-index index))
  1752.       (write-byte (aref vector index) stream))))))
  1753.  
  1754. ;;; buffer-force-output-default - force output to the X stream
  1755.  
  1756. #+excl
  1757. (defun buffer-force-output-default (display)
  1758.   ;; buffer-write-default does the actual writing.
  1759.   (declare (ignore display)))
  1760.  
  1761. #-excl
  1762. (defun buffer-force-output-default (display)
  1763.   ;; The default buffer force-output function for use with common-lisp streams
  1764.   (declare (type display display))
  1765.   (let ((stream (display-output-stream display)))
  1766.     (declare (type (or null stream) stream))
  1767.     (unless (null stream)
  1768.       (force-output stream))))
  1769.  
  1770. ;;; BUFFER-CLOSE-DEFAULT - close the X stream
  1771.  
  1772. #+excl
  1773. (defun buffer-close-default (display &key abort)
  1774.   ;; The default buffer close function for use with common-lisp streams
  1775.   (declare (type display display)
  1776.        (ignore abort))
  1777.   #.(declare-buffun)
  1778.   (excl::filesys-checking-close (display-output-stream display)))
  1779.  
  1780. #-excl
  1781. (defun buffer-close-default (display &key abort)
  1782.   ;; The default buffer close function for use with common-lisp streams
  1783.   (declare (type display display))
  1784.   #.(declare-buffun)
  1785.   (let ((stream (display-output-stream display)))
  1786.     (declare (type (or null stream) stream))
  1787.     (unless (null stream)
  1788.       (close stream :abort abort))))
  1789.  
  1790. ;;; BUFFER-INPUT-WAIT-DEFAULT - wait for for input to be available for the
  1791. ;;; buffer.  This is called in read-input between requests, so that a process
  1792. ;;; waiting for input is abortable when between requests.  Should return
  1793. ;;; :TIMEOUT if it times out, NIL otherwise.
  1794.  
  1795. ;;; The default implementation
  1796.  
  1797. ;; Poll for input every *buffer-read-polling-time* SECONDS.
  1798. #-(or Genera explorer excl lcl3.0 CMU)
  1799. (defparameter *buffer-read-polling-time* 0.5)
  1800.  
  1801. #-(or Genera explorer excl lcl3.0 CMU)
  1802. (defun buffer-input-wait-default (display timeout)
  1803.   (declare (type display display)
  1804.        (type (or null number) timeout))
  1805.   (declare (values timeout))
  1806.   
  1807.   (let ((stream (display-input-stream display)))
  1808.     (declare (type (or null stream) stream))
  1809.     (cond ((null stream))
  1810.       ((listen stream) nil)
  1811.       ((eql timeout 0) :timeout)
  1812.       ((not (null timeout))
  1813.        (multiple-value-bind (npoll fraction)
  1814.            (truncate timeout *buffer-read-polling-time*)
  1815.          (dotimes (i npoll)            ; Sleep for a time, then listen again
  1816.            (sleep *buffer-read-polling-time*)
  1817.            (when (listen stream)
  1818.          (return-from buffer-input-wait-default nil)))
  1819.          (when (plusp fraction)
  1820.            (sleep fraction)            ; Sleep a fraction of a second
  1821.            (when (listen stream)        ; and listen one last time
  1822.          (return-from buffer-input-wait-default nil)))
  1823.          :timeout)))))
  1824.  
  1825. #+Genera
  1826. (defun buffer-input-wait-default (display timeout)
  1827.   (declare (type display display)
  1828.        (type (or null number) timeout))
  1829.   (declare (values timeout))
  1830.   (let ((stream (display-input-stream display)))
  1831.     (declare (type (or null stream) stream))
  1832.     (cond ((null stream))
  1833.       ((scl:send stream :listen) nil)
  1834.       ((eql timeout 0) :timeout)
  1835.       ((null timeout) (si:stream-input-block stream "CLX Input"))
  1836.       (t
  1837.        (scl:condition-bind ((neti:protocol-timeout
  1838.                   #'(lambda (error)
  1839.                       (when (eq stream (scl:send error :stream))
  1840.                     (return-from buffer-input-wait-default :timeout)))))
  1841.          (neti:with-stream-timeout (stream :input timeout)
  1842.            (si:stream-input-block stream "CLX Input")))))
  1843.     nil))
  1844.  
  1845. #+explorer
  1846. (defun buffer-input-wait-default (display timeout)
  1847.   (declare (type display display)
  1848.        (type (or null number) timeout))
  1849.   (declare (values timeout))
  1850.   (let ((stream (display-input-stream display)))
  1851.     (declare (type (or null stream) stream))
  1852.     (cond ((null stream))
  1853.       ((zl:send stream :listen) nil)
  1854.       ((eql timeout 0) :timeout)
  1855.       ((null timeout)
  1856.        (si:process-wait "CLX Input" stream :listen))
  1857.       (t
  1858.        (unless (si:process-wait-with-timeout
  1859.                "CLX Input" (round (* timeout 60.)) stream :listen)
  1860.          (return-from buffer-input-wait-default :timeout))))
  1861.     nil))
  1862.  
  1863. #+excl
  1864. ;;
  1865. ;; This is used so an 'eq' test may be used to find out whether or not we can
  1866. ;; safely throw this process out of the CLX read loop.
  1867. ;;
  1868. (defparameter *read-whostate* "waiting for input from X server")
  1869.  
  1870. ;;
  1871. ;; Note that this function returns nil on error if the scheduler is running,
  1872. ;; t on error if not.  This is ok since buffer-read will detect the error.
  1873. ;;
  1874. #+excl
  1875. (defun buffer-input-wait-default (display timeout)
  1876.   (declare (type display display)
  1877.        (type (or null number) timeout))
  1878.   (declare (values timeout))
  1879.   (let ((fd (display-input-stream display)))
  1880.     (declare (fixnum fd))
  1881.     (when (>= fd 0)
  1882.       (cond ((fd-char-avail-p fd)
  1883.          nil)
  1884.         
  1885.         ;; Otherwise no bytes were available on the socket
  1886.         ((and timeout (zerop timeout))
  1887.          ;; If there aren't enough and timeout == 0, timeout.
  1888.          :timeout)
  1889.       
  1890.         ;; If the scheduler is running let it do timeouts.
  1891.         (mp::*scheduler-stack-group*
  1892.          #+allegro
  1893.          (if (not
  1894.           (mp:wait-for-input-available fd :whostate *read-whostate*
  1895.                            :wait-function #'fd-char-avail-p
  1896.                            :timeout timeout))
  1897.          (return-from buffer-input-wait-default :timeout))
  1898.          #-allegro
  1899.          (mp::wait-for-input-available fd :whostate *read-whostate*
  1900.                        :wait-function #'fd-char-avail-p))
  1901.         
  1902.         ;; Otherwise we have to handle timeouts by hand, and call select()
  1903.         ;; to block until input is available.  Note we don't really handle
  1904.         ;; the interaction of interrupts and (numberp timeout) here.  XX
  1905.         (t
  1906.          (let ((res 0))
  1907.            (declare (fixnum res))
  1908.            (with-interrupt-checking-on
  1909.         (loop
  1910.           (setq res (fd-wait-for-input fd (if (null timeout) 0
  1911.                             (truncate timeout))))
  1912.           (cond ((plusp res)    ; success
  1913.              (return nil))
  1914.             ((eq res 0)    ; timeout
  1915.              (return :timeout))
  1916.             ((eq res -1)    ; error
  1917.              (return t))
  1918.             ;; Otherwise we got an interrupt -- go around again.
  1919.             )))))))))
  1920.  
  1921.        
  1922. #+lcl3.0
  1923. (defun buffer-input-wait-default (display timeout)
  1924.   (declare (type display display)
  1925.        (type (or null number) timeout)
  1926.        (values timeout))
  1927.   #.(declare-buffun)
  1928.   (let ((stream (display-input-stream display)))
  1929.     (declare (type (or null stream) stream))
  1930.     (cond ((null stream))
  1931.       ((listen stream) nil)
  1932.       ((eql timeout 0) :timeout)
  1933.       ((with-underlying-stream (stream stream display input)
  1934.          (lucid::waiting-for-input-from-stream stream
  1935.                (lucid::with-io-unlocked
  1936.          (if (null timeout)
  1937.              (lcl:process-wait "CLX Input" #'listen stream)
  1938.            (lcl:process-wait-with-timeout
  1939.              "CLX Input" timeout #'listen stream)))))
  1940.        nil)
  1941.       (:timeout))))
  1942.  
  1943. #+CMU
  1944. (defun buffer-input-wait-default (display timeout)
  1945.   (declare (type display display)
  1946.        (type (or null number) timeout))
  1947.   (let ((stream (display-input-stream display)))
  1948.     (declare (type (or null stream) stream))
  1949.     (cond ((null stream))
  1950.       ((listen stream) nil)
  1951.       ((eql timeout 0) :timeout)
  1952.       (t
  1953.        (if (system:wait-until-fd-usable (system:fd-stream-fd stream)
  1954.                         :input timeout)
  1955.            nil
  1956.            :timeout)))))
  1957.  
  1958. ;;; BUFFER-LISTEN-DEFAULT - returns T if there is input available for the
  1959. ;;; buffer. This should never block, so it can be called from the scheduler.
  1960.  
  1961. ;;; The default implementation is to just use listen.
  1962. #-excl
  1963. (defun buffer-listen-default (display)
  1964.   (declare (type display display))
  1965.   (let ((stream (display-input-stream display)))
  1966.     (declare (type (or null stream) stream))
  1967.     (if (null stream)
  1968.     t
  1969.       (listen stream))))
  1970.  
  1971. #+excl 
  1972. (defun buffer-listen-default (display)
  1973.   (declare (type display display))
  1974.   (let ((fd (display-input-stream display)))
  1975.     (declare (type fixnum fd))
  1976.     (if (= fd -1)
  1977.     t
  1978.       (fd-char-avail-p fd))))
  1979.  
  1980.  
  1981. ;;;----------------------------------------------------------------------------
  1982. ;;; System dependent speed hacks
  1983. ;;;----------------------------------------------------------------------------
  1984.  
  1985. ;;
  1986. ;; WITH-STACK-LIST is used by WITH-STATE as a memory saving feature.
  1987. ;; If your lisp doesn't have stack-lists, and you're worried about
  1988. ;; consing garbage, you may want to re-write this to allocate and
  1989. ;; initialize lists from a resource.
  1990. ;;
  1991. #-lispm
  1992. (defmacro with-stack-list ((var &rest elements) &body body)
  1993.   ;; SYNTAX: (WITH-STACK-LIST (var exp1 ... expN) body)
  1994.   ;; Equivalent to (LET ((var (MAPCAR #'EVAL '(exp1 ... expN)))) body)
  1995.   ;; except that the list produced by MAPCAR resides on the stack and
  1996.   ;; therefore DISAPPEARS when WITH-STACK-LIST is exited.
  1997.   `(let ((,var (list ,@elements)))
  1998.      (declare (type cons ,var)
  1999.           #+clx-ansi-common-lisp (dynamic-extent ,var))
  2000.      ,@body))
  2001.  
  2002. #-lispm
  2003. (defmacro with-stack-list* ((var &rest elements) &body body)
  2004.   ;; SYNTAX: (WITH-STACK-LIST* (var exp1 ... expN) body)
  2005.   ;; Equivalent to (LET ((var (APPLY #'LIST* (MAPCAR #'EVAL '(exp1 ... expN))))) body)
  2006.   ;; except that the list produced by MAPCAR resides on the stack and
  2007.   ;; therefore DISAPPEARS when WITH-STACK-LIST is exited.
  2008.   `(let ((,var (list* ,@elements)))
  2009.      (declare (type cons ,var)
  2010.           #+clx-ansi-common-lisp (dynamic-extent ,var))
  2011.      ,@body))
  2012.  
  2013. (declaim (inline buffer-replace))
  2014.  
  2015. #+lispm
  2016. (defun buffer-replace (buf1 buf2 start1 end1 &optional (start2 0))
  2017.   (declare (type vector buf1 buf2)
  2018.        (type array-index start1 end1 start2))
  2019.   (sys:copy-array-portion buf2 start2 (length buf2) buf1 start1 end1))
  2020.  
  2021. #+excl
  2022. (defun buffer-replace (target-sequence source-sequence target-start
  2023.                        target-end &optional (source-start 0))
  2024.   (declare (type buffer-bytes target-sequence source-sequence)
  2025.        (type array-index target-start target-end source-start)
  2026.        (optimize (speed 3) (safety 0)))
  2027.   
  2028.   (let ((source-end (length source-sequence)))
  2029.     (declare (type array-index source-end))
  2030.     
  2031.     (excl:if* (and (eq target-sequence source-sequence)
  2032.            (> target-start source-start))
  2033.        then (let ((nelts (min (- target-end target-start)
  2034.                   (- source-end source-start))))
  2035.           (do ((target-index (+ target-start nelts -1) (1- target-index))
  2036.            (source-index (+ source-start nelts -1) (1- source-index)))
  2037.           ((= target-index (1- target-start)) target-sequence)
  2038.         (declare (type array-index target-index source-index))
  2039.         
  2040.         (setf (aref target-sequence target-index)
  2041.           (aref source-sequence source-index))))
  2042.        else (do ((target-index target-start (1+ target-index))
  2043.          (source-index source-start (1+ source-index)))
  2044.         ((or (= target-index target-end) (= source-index source-end))
  2045.          target-sequence)
  2046.           (declare (type array-index target-index source-index))
  2047.  
  2048.           (setf (aref target-sequence target-index)
  2049.         (aref source-sequence source-index))))))
  2050.  
  2051. #+lucid
  2052. ;;;The compiler is *supposed* to optimize calls to replace, but in actual
  2053. ;;;fact it does not.
  2054. (defun buffer-replace (buf1 buf2 start1 end1 &optional (start2 0))
  2055.   (declare (type buffer-bytes buf1 buf2)
  2056.        (type array-index start1 end1 start2))
  2057.   #.(declare-buffun)
  2058.   (let ((end2 (lucid::%simple-8bit-vector-length buf2)))
  2059.     (declare (type array-index end2))
  2060.     (lucid::simple-8bit-vector-replace-internal
  2061.       buf1 buf2 start1 end1 start2 end2)))
  2062.  
  2063. #+cmu
  2064. (defun buffer-replace (buf1 buf2 start1 end1 &optional (start2 0))
  2065.   (declare (type buffer-bytes buf1 buf2)
  2066.        (type array-index start1 end1 start2))
  2067.   #.(declare-buffun)
  2068.   (kernel:bit-bash-copy
  2069.    buf2 (+ (* start2 vm:byte-bits)
  2070.        (* vm:vector-data-offset vm:word-bits))
  2071.    buf1 (+ (* start1 vm:byte-bits)
  2072.        (* vm:vector-data-offset vm:word-bits))
  2073.    (* (- end1 start1) vm:byte-bits)))
  2074.  
  2075. #+(and clx-overlapping-arrays (not (or lispm lucid excl cmu)))
  2076. (defun buffer-replace (buf1 buf2 start1 end1 &optional (start2 0))
  2077.   (declare (type vector buf1 buf2)
  2078.        (type array-index start1 end1 start2))
  2079.   (replace buf1 buf2 :start1 start1 :end1 end1 :start2 start2))
  2080.  
  2081. #-(or lispm lucid excl cmu clx-overlapping-arrays)
  2082. (defun buffer-replace (buf1 buf2 start1 end1 &optional (start2 0))
  2083.   (declare (type buffer-bytes buf1 buf2)
  2084.        (type array-index start1 end1 start2))
  2085.   (replace buf1 buf2 :start1 start1 :end1 end1 :start2 start2))
  2086.  
  2087. #+ti
  2088. (defun with-location-bindings (sys:"e bindings &rest body)
  2089.   (do ((bindings bindings (cdr bindings)))
  2090.       ((null bindings)
  2091.        (sys:eval-body-as-progn body))
  2092.     (sys:bind (sys:*eval `(sys:locf ,(caar bindings)))
  2093.           (sys:*eval (cadar bindings)))))
  2094.  
  2095. #+ti
  2096. (compiler:defoptimizer with-location-bindings with-l-b-compiler nil (form)
  2097.   (let ((bindings (cadr form))
  2098.     (body (cddr form)))
  2099.     `(let ()
  2100.        ,@(loop for (accessor value) in bindings
  2101.            collect `(si:bind (si:locf ,accessor) ,value))
  2102.        ,@body)))
  2103.  
  2104. #+ti
  2105. (defun (:property with-location-bindings compiler::cw-handler) (exp)
  2106.   (let* ((bindlist (mapcar #'compiler::cw-clause (second exp)))
  2107.      (body (compiler::cw-clause (cddr exp))))
  2108.     (and compiler::cw-return-expansion-flag
  2109.      (list* (first exp) bindlist body))))
  2110.  
  2111. #+(and lispm (not ti))
  2112. (defmacro with-location-bindings (bindings &body body)
  2113.   `(sys:letf* ,bindings ,@body))
  2114.  
  2115. #+lispm
  2116. (defmacro with-gcontext-bindings ((gc saved-state indexes ts-index temp-mask temp-gc)
  2117.                   &body body)
  2118.   ;; don't use svref on LHS because Symbolics didn't define locf for it
  2119.   (let* ((local-state (gensym))
  2120.      (bindings `(((aref ,local-state ,ts-index) 0))))    ; will become zero anyway
  2121.     (dolist (index indexes)
  2122.       (push `((aref ,local-state ,index) (svref ,saved-state ,index))
  2123.         bindings))
  2124.     `(let ((,local-state (gcontext-local-state ,gc)))
  2125.        (declare (type gcontext-state ,local-state))
  2126.        (unwind-protect
  2127.        (with-location-bindings ,bindings
  2128.          ,@body)
  2129.      (setf (svref ,local-state ,ts-index) 0)
  2130.      (when ,temp-gc
  2131.        (restore-gcontext-temp-state ,gc ,temp-mask ,temp-gc))
  2132.      (deallocate-gcontext-state ,saved-state)))))
  2133.  
  2134. #-lispm
  2135. (defmacro with-gcontext-bindings ((gc saved-state indexes ts-index temp-mask temp-gc)
  2136.                   &body body)
  2137.   (let ((local-state (gensym))
  2138.     (resets nil))
  2139.     (dolist (index indexes)
  2140.       (push `(setf (svref ,local-state ,index) (svref ,saved-state ,index))
  2141.         resets))
  2142.     `(unwind-protect
  2143.      (progn
  2144.        ,@body)
  2145.        (let ((,local-state (gcontext-local-state ,gc)))
  2146.      (declare (type gcontext-state ,local-state))
  2147.      ,@resets
  2148.      (setf (svref ,local-state ,ts-index) 0))
  2149.        (when ,temp-gc
  2150.      (restore-gcontext-temp-state ,gc ,temp-mask ,temp-gc))
  2151.        (deallocate-gcontext-state ,saved-state))))
  2152.  
  2153. ;;;----------------------------------------------------------------------------
  2154. ;;; How error detection should CLX do?
  2155. ;;; Several levels are possible:
  2156. ;;;
  2157. ;;; 1. Do the equivalent of check-type on every argument.
  2158. ;;; 
  2159. ;;; 2. Simply report TYPE-ERROR.  This eliminates overhead of all the format
  2160. ;;;    strings generated by check-type.
  2161. ;;; 
  2162. ;;; 3. Do error checking only on arguments that are likely to have errors
  2163. ;;;    (like keyword names)
  2164. ;;; 
  2165. ;;; 4. Do error checking only where not doing so may dammage the envirnment
  2166. ;;;    on a non-tagged machine (i.e. when storing into a structure that has
  2167. ;;;    been passed in)
  2168. ;;; 
  2169. ;;; 5. No extra error detection code.  On lispm's, ASET may barf trying to
  2170. ;;;    store a non-integer into a number array. 
  2171. ;;; 
  2172. ;;; How extensive should the error checking be?  For example, if the server
  2173. ;;; expects a CARD16, is is sufficient for CLX to check for integer, or
  2174. ;;; should it also check for non-negative and less than 65536?
  2175. ;;;----------------------------------------------------------------------------
  2176.  
  2177. ;; The *TYPE-CHECK?* constant controls how much error checking is done.
  2178. ;; Possible values are:
  2179. ;;    NIL      - Don't do any error checking
  2180. ;;    t        - Do the equivalent of checktype on every argument
  2181. ;;    :minimal - Do error checking only where errors are likely
  2182.  
  2183. ;;; This controls macro expansion, and isn't changable at run-time You will
  2184. ;;; probably want to set this to nil if you want good performance at
  2185. ;;; production time.
  2186. (defconstant *type-check?* #+(or Genera Minima cmu) nil #-(or Genera Minima cmu) t)
  2187.  
  2188. ;; TYPE? is used to allow the code to do error checking at a different level from
  2189. ;; the declarations.  It also does some optimizations for systems that don't have
  2190. ;; good compiler support for TYPEP.  The definitions for CARD32, CARD16, INT16, etc.
  2191. ;; include range checks.  You can modify TYPE? to do less extensive checking
  2192. ;; for these types if you desire.
  2193. ;; ### This comment is a lie!  TYPE? is really also used for run-time type
  2194. ;; dispatching, not just type checking.  -- Ram.
  2195.  
  2196. (defmacro type? (object type)
  2197.   #+cmu
  2198.   `(typep ,object ,type)
  2199.   #-cmu
  2200.   (if (not (constantp type))
  2201.       `(typep ,object ,type)
  2202.     (progn
  2203.       (setq type (eval type))
  2204.       #+(or Genera explorer Minima)
  2205.       (if *type-check?*
  2206.       `(locally (declare (optimize safety)) (typep ,object ',type))
  2207.     `(typep ,object ',type))
  2208.       #-(or Genera explorer Minima)
  2209.       (let ((predicate (assoc type
  2210.                   '((drawable drawable-p) (window window-p)
  2211.                 (pixmap pixmap-p) (cursor cursor-p)
  2212.                 (font font-p) (gcontext gcontext-p)
  2213.                 (colormap colormap-p) (null null)
  2214.                 (integer integerp)))))
  2215.     (cond (predicate
  2216.            `(,(second predicate) ,object))
  2217.           ((eq type 'boolean)
  2218.            't)            ; Everything is a boolean.
  2219.           (*type-check?*
  2220.            `(locally (declare (optimize safety)) (typep ,object ',type)))
  2221.           (t
  2222.            `(typep ,object ',type)))))))
  2223.  
  2224. ;; X-TYPE-ERROR is the function called for type errors.
  2225. ;; If you want lots of checking, but are concerned about code size,
  2226. ;; this can be made into a macro that ignores some parameters.
  2227.  
  2228. (defun x-type-error (object type &optional error-string)
  2229.   (x-error 'x-type-error
  2230.        :datum object
  2231.        :expected-type type
  2232.        :type-string error-string))
  2233.  
  2234.  
  2235. ;;-----------------------------------------------------------------------------
  2236. ;; Error handlers
  2237. ;;    Hack up KMP error signaling using zetalisp until the real thing comes 
  2238. ;;    along
  2239. ;;-----------------------------------------------------------------------------
  2240.  
  2241. (defun default-error-handler (display error-key &rest key-vals
  2242.                   &key asynchronous &allow-other-keys)
  2243.   (declare (type boolean asynchronous)
  2244.        (dynamic-extent key-vals))
  2245.   ;; The default display-error-handler.
  2246.   ;; It signals the conditions listed in the DISPLAY file.
  2247.   (if asynchronous
  2248.       (apply #'x-cerror "Ignore" error-key :display display :error-key error-key key-vals)
  2249.       (apply #'x-error error-key :display display :error-key error-key key-vals)))
  2250.  
  2251. #+(and lispm (not Genera) (not clx-ansi-common-lisp))
  2252. (defun x-error (condition &rest keyargs)
  2253.   (apply #'sys:signal condition keyargs))
  2254.  
  2255. #+(and lispm (not Genera) (not clx-ansi-common-lisp))
  2256. (defun x-cerror (proceed-format-string condition &rest keyargs)
  2257.   (sys:signal (apply #'zl:make-condition condition keyargs)
  2258.           :proceed-types proceed-format-string))
  2259.  
  2260. #+(and Genera (not clx-ansi-common-lisp))
  2261. (defun x-error (condition &rest keyargs)
  2262.   (declare (dbg:error-reporter))
  2263.   (apply #'sys:signal condition keyargs))
  2264.  
  2265. #+(and Genera (not clx-ansi-common-lisp))
  2266. (defun x-cerror (proceed-format-string condition &rest keyargs)
  2267.   (declare (dbg:error-reporter))
  2268.   (apply #'sys:signal condition :continue-format-string proceed-format-string keyargs))
  2269.  
  2270. #+(or clx-ansi-common-lisp excl lcl3.0 (and kcl clos-conditions))
  2271. (defun x-error (condition &rest keyargs)
  2272.   #-kcl (declare (dynamic-extent keyargs))
  2273.   (apply #'error condition keyargs))
  2274.  
  2275. #+(or clx-ansi-common-lisp excl lcl3.0 CMU (and kcl clos-conditions))
  2276. (defun x-cerror (proceed-format-string condition &rest keyargs)
  2277.   #-kcl (declare (dynamic-extent keyargs))
  2278.   (apply #'cerror proceed-format-string condition keyargs))
  2279.  
  2280. ;;; X-ERROR for CMU Common Lisp
  2281. ;;;
  2282. ;;; We detect a couple condition types for which we disable event handling in
  2283. ;;; our system.  This prevents going into the debugger or returning to a
  2284. ;;; command prompt with CLX repeatedly seeing the same condition.  This occurs
  2285. ;;; because CMU Common Lisp provides for all events (that is, X, input on file
  2286. ;;; descriptors, Mach messages, etc.) to come through one routine anyone can
  2287. ;;; use to wait for input.
  2288. ;;;
  2289. #+CMU
  2290. (defun x-error (condition &rest keyargs)
  2291.   (let ((condx (apply #'make-condition condition keyargs)))
  2292.     (when (and (eq condition 'closed-display)
  2293.            (fboundp 'ext::disable-clx-event-handling))
  2294.       (let ((disp (closed-display-display condx)))
  2295.     (warn "Disabled event handling on ~S." disp)
  2296.     (ext::disable-clx-event-handling disp)))
  2297.     (error condx)))
  2298.  
  2299. #-(or lispm ansi-common-lisp excl lcl3.0 CMU (and kcl clos-conditions))
  2300. (defun x-error (condition &rest keyargs)
  2301.   (error "X-Error: ~a"
  2302.      (princ-to-string (apply #'make-condition condition keyargs))))
  2303.  
  2304. #-(or lispm clx-ansi-common-lisp excl lcl3.0 CMU (and kcl clos-conditions))
  2305. (defun x-cerror (proceed-format-string condition &rest keyargs)
  2306.   (cerror proceed-format-string "X-Error: ~a"
  2307.      (princ-to-string (apply #'make-condition condition keyargs))))
  2308.  
  2309. ;; version 15 of Pitman error handling defines the syntax for define-condition to be:
  2310. ;; DEFINE-CONDITION name (parent-type) [({slot}*) {option}*]
  2311. ;; Where option is one of: (:documentation doc-string) (:conc-name symbol-or-string)
  2312. ;; or (:report exp)
  2313.  
  2314. #+lcl3.0 
  2315. (defmacro define-condition (name parent-types &optional slots &rest args)
  2316.   `(lcl:define-condition
  2317.      ,name (,(first parent-types))
  2318.      ,(mapcar #'(lambda (slot) (if (consp slot) (car slot) slot))
  2319.           slots)
  2320.      ,@args))
  2321.  
  2322. #+(and excl (not clx-ansi-common-lisp))
  2323. (defmacro define-condition (name parent-types &optional slots &rest args)
  2324.   `(excl::define-condition
  2325.      ,name (,(first parent-types))
  2326.      ,(mapcar #'(lambda (slot) (if (consp slot) (car slot) slot))
  2327.           slots)
  2328.      ,@args))
  2329.  
  2330. #+(and lispm (not clx-ansi-common-lisp))
  2331. (defmacro define-condition (name parent-types &body options)
  2332.   (let ((slot-names
  2333.       (mapcar #'(lambda (slot) (if (consp slot) (car slot) slot))
  2334.           (pop options)))
  2335.     (documentation nil)
  2336.     (conc-name (concatenate 'string (string name) "-"))           
  2337.     (reporter nil))
  2338.     (dolist (item options)
  2339.       (ecase (first item)
  2340.     (:documentation (setq documentation (second item)))
  2341.     (:conc-name (setq conc-name (string (second item))))
  2342.     (:report (setq reporter (second item)))))
  2343.     `(within-definition (,name define-condition)
  2344.        (zl:defflavor ,name ,slot-names ,parent-types
  2345.      :initable-instance-variables
  2346.      #-Genera
  2347.      (:accessor-prefix ,conc-name)
  2348.      #+Genera
  2349.      (:conc-name ,conc-name)
  2350.      #-Genera
  2351.      (:outside-accessible-instance-variables ,@slot-names)
  2352.      #+Genera
  2353.      (:readable-instance-variables ,@slot-names))
  2354.        ,(when reporter ;; when no reporter, parent's is inherited
  2355.       `(zl:defmethod #-Genera (,name :report)
  2356.                      #+Genera (dbg:report ,name) (stream)
  2357.           ,(if (stringp reporter)
  2358.            `(write-string ,reporter stream)
  2359.          `(,reporter global:self stream))
  2360.           global:self))
  2361.        (zl:compile-flavor-methods ,name)
  2362.        ,(when documentation
  2363.       `(setf (documentation name 'type) ,documentation))
  2364.        ',name)))
  2365.  
  2366. #+(and lispm (not Genera) (not clx-ansi-common-lisp))
  2367. (zl:defflavor x-error () (global:error))
  2368.  
  2369. #+(and Genera (not clx-ansi-common-lisp))
  2370. (scl:defflavor x-error
  2371.     ((dbg:proceed-types '(:continue))    ;
  2372.      continue-format-string)
  2373.     (sys:error)
  2374.   (:initable-instance-variables continue-format-string))
  2375.  
  2376. #+(and Genera (not clx-ansi-common-lisp))
  2377. (scl:defmethod (scl:make-instance x-error) (&rest ignore)
  2378.   (when (not (sys:variable-boundp continue-format-string))
  2379.     (setf dbg:proceed-types (remove :continue dbg:proceed-types))))
  2380.  
  2381. #+(and Genera (not clx-ansi-common-lisp))
  2382. (scl:defmethod (dbg:proceed x-error :continue) ()
  2383.   :continue)
  2384.  
  2385. #+(and Genera (not clx-ansi-common-lisp))
  2386. (sys:defmethod (dbg:document-proceed-type x-error :continue) (stream)
  2387.   (format stream continue-format-string))
  2388.  
  2389. #+(or clx-ansi-common-lisp excl lcl3.0 CMU (and kcl clos-conditions))
  2390. (define-condition x-error (error) ())
  2391.  
  2392. #-(or lispm clx-ansi-common-lisp excl lcl3.0 CMU (and kcl clos-conditions))
  2393. (defstruct x-error
  2394.   report-function)
  2395.  
  2396. #-(or lispm clx-ansi-common-lisp excl lcl3.0 CMU (and kcl clos-conditions))
  2397. (defmacro define-condition (name parent-types &body options)
  2398.   ;; Define a structure that when printed displays an error message
  2399.   (flet ((reporter-for-condition (name)
  2400.        (xintern "." name '-reporter.)))
  2401.     (let ((slot-names
  2402.         (mapcar #'(lambda (slot) (if (consp slot) (car slot) slot))
  2403.             (pop options)))
  2404.       (documentation nil)
  2405.       (conc-name (concatenate 'string (string name) "-"))           
  2406.       (reporter nil)
  2407.       (condition (gensym))
  2408.       (stream (gensym))
  2409.       (report-function (reporter-for-condition name)))
  2410.       (dolist (item options)
  2411.     (ecase (first item)
  2412.       (:documentation (setq documentation (second item)))
  2413.       (:conc-name (setq conc-name (string (second item))))
  2414.       (:report (setq reporter (second item)))))
  2415.       (unless reporter
  2416.     (setq report-function (reporter-for-condition (first parent-types))))
  2417.       `(within-definition (,name define-condition)
  2418.      (defstruct (,name (:conc-name ,(intern conc-name))
  2419.              (:print-function condition-print)
  2420.              (:include ,(first parent-types)
  2421.               (report-function ',report-function)))
  2422.        ,@slot-names)
  2423.      ,(when documentation
  2424.         `(setf (documentation name 'type) ,documentation))
  2425.      ,(when reporter
  2426.         `(defun ,report-function (,condition ,stream)
  2427.            ,(if (stringp reporter)
  2428.             `(write-string ,reporter ,stream)
  2429.           `(,reporter ,condition ,stream))
  2430.            ,condition))
  2431.      ',name))))
  2432.  
  2433. #-(or lispm clx-ansi-common-lisp excl lcl3.0 CMU (and kcl clos-conditions))
  2434. (defun condition-print (condition stream depth)
  2435.   (declare (type x-error condition)
  2436.        (type stream stream)
  2437.        (ignore depth))
  2438.   (if *print-escape*
  2439.       (print-unreadable-object (condition stream :type t))
  2440.     (funcall (x-error-report-function condition) condition stream))
  2441.   condition)
  2442.   
  2443. #-(or lispm clx-ansi-common-lisp excl lcl3.0 CMU (and kcl clos-conditions))
  2444. (defun make-condition (type &rest slot-initializations)
  2445.   (declare (dynamic-extent slot-initializations))
  2446.   (let ((make-function (intern (concatenate 'string (string 'make-) (string type))
  2447.                    (symbol-package type))))
  2448.     (apply make-function slot-initializations)))
  2449.  
  2450. #-(or clx-ansi-common-lisp excl lcl3.0 CMU (and kcl clos-conditions))
  2451. (define-condition type-error (x-error)
  2452.   ((datum :reader type-error-datum :initarg :datum)
  2453.    (expected-type :reader type-error-expected-type :initarg :expected-type))
  2454.   (:report
  2455.     (lambda (condition stream)
  2456.       (format stream "~s isn't a ~a"
  2457.           (type-error-datum condition)
  2458.           (type-error-expected-type condition)))))
  2459.  
  2460.  
  2461. ;;-----------------------------------------------------------------------------
  2462. ;;  HOST hacking
  2463. ;;-----------------------------------------------------------------------------
  2464.  
  2465. #-(or explorer Genera Minima)
  2466. (defun host-address (host &optional (family :internet))
  2467.   ;; Return a list whose car is the family keyword (:internet :DECnet :Chaos)
  2468.   ;; and cdr is a list of network address bytes.
  2469.   (declare (type (or stringable list) host)
  2470.        (type (or null (member :internet :decnet :chaos) card8) family))
  2471.   (declare (values list))
  2472.   host family
  2473.   (error "HOST-ADDRESS not implemented yet."))
  2474.  
  2475. #+explorer
  2476. (defun host-address (host &optional (family :internet))
  2477.   ;; Return a list whose car is the family keyword (:internet :DECnet :Chaos)
  2478.   ;; and cdr is a list of network address bytes.
  2479.   (declare (type (or stringable list) host)
  2480.        (type (or null (member :internet :decnet :chaos) card8) family))
  2481.   (declare (values list))
  2482.   (ecase family
  2483.     (:internet
  2484.      (let ((addr (ip:get-ip-address host)))
  2485.        (unless addr (error "~s isn't an internet host name" host))
  2486.        (list :internet
  2487.          (ldb (byte 8 24) addr)
  2488.          (ldb (byte 8 16) addr)
  2489.          (ldb (byte 8 8) addr)
  2490.          (ldb (byte 8 0) addr))))
  2491.     (:chaos
  2492.      (let ((addr (first (chaos:chaos-addresses host))))
  2493.        (unless addr (error "~s isn't a chaos host name" host))
  2494.        (list :chaos
  2495.          (ldb (byte 8 0) addr)
  2496.          (ldb (byte 8 8) addr))))))
  2497.  
  2498. #+Genera
  2499. (defun host-address (host &optional (family :internet))
  2500.   ;; Return a list whose car is the family keyword (:internet :DECnet :Chaos)
  2501.   ;; and cdr is a list of network address bytes.
  2502.   (declare (type (or stringable list) host)
  2503.        (type (or null (member :internet :decnet :chaos) card8) family))
  2504.   (declare (values list))
  2505.   (let ((net-type (if (eq family :DECnet)
  2506.               :DNA
  2507.               family)))
  2508.     (dolist (addr
  2509.           (sys:send (net:parse-host host) :network-addresses)
  2510.           (error "~s isn't a valid ~(~A~) host name" host family))
  2511.       (let ((network (car addr))
  2512.         (address (cadr addr)))
  2513.     (when (sys:send network :network-typep net-type)
  2514.       (return (ecase family
  2515.             (:internet
  2516.               (multiple-value-bind (a b c d) (tcp:explode-internet-address address)
  2517.             (list :internet a b c d)))
  2518.             ((:chaos :DECnet)
  2519.              (list family (ldb (byte 8 0) address) (ldb (byte 8 8) address))))))))))
  2520.  
  2521. #+Minima
  2522. (defun host-address (host &optional (family :internet))
  2523.   ;; Return a list whose car is the family keyword (:internet :DECnet :Chaos)
  2524.   ;; and cdr is a list of network address bytes.
  2525.   (declare (type (or stringable list) host)
  2526.        (type (or null (member :internet :decnet :chaos) card8) family))
  2527.   (declare (values list))
  2528.   (check-type family (member :internet))
  2529.   (or (loop with (num delim)
  2530.         repeat 4
  2531.         for idx = 0 then (1+ delim)
  2532.         when (and delim (or (= delim (length host))
  2533.                 (char-not-equal (char host delim) #\.)))
  2534.           return nil
  2535.         do (multiple-value-setq (num delim)
  2536.              (parse-integer host :start idx :junk-allowed t))
  2537.         when (or (null num) (< num 0) (> num 255))
  2538.           return nil
  2539.         else collect num into nums
  2540.         finally (return (when (= delim (length host))
  2541.                   (cons :internet nums))))
  2542.       (error "Invalid internet address [~A]." host)))
  2543.  
  2544. #+explorer ;; This isn't required, but it helps make sense of the results from access-hosts
  2545. (defun get-host (host-object)
  2546.   ;; host-object is a list whose car is the family keyword (:internet :DECnet :Chaos)
  2547.   ;; and cdr is a list of network address bytes.
  2548.   (declare (type list host-object))
  2549.   (declare (values string family))
  2550.   (let* ((family (first host-object))
  2551.      (address (ecase family
  2552.             (:internet
  2553.              (dpb (second host-object)
  2554.               (byte 8 24)
  2555.               (dpb (third host-object)
  2556.                    (byte 8 16)
  2557.                    (dpb (fourth host-object)
  2558.                     (byte 8 8)
  2559.                     (fifth host-object)))))
  2560.             (:chaos
  2561.              (dpb (third host-object) (byte 8 8) (second host-object))))))
  2562.     (when (eq family :internet) (setq family :ip))
  2563.     (let ((host (si:get-host-from-address address family)))
  2564.       (values (and host (funcall host :name)) family))))
  2565.  
  2566. ;;; This isn't required, but it helps make sense of the results from access-hosts
  2567. #+Genera
  2568. (defun get-host (host-object)
  2569.   ;; host-object is a list whose car is the family keyword (:internet :DECnet :Chaos)
  2570.   ;; and cdr is a list of network address bytes.
  2571.   (declare (type list host-object))
  2572.   (declare (values string family))
  2573.   (let ((family (first host-object)))
  2574.     (values (sys:send (net:get-host-from-address 
  2575.             (ecase family
  2576.               (:internet
  2577.                 (apply #'tcp:build-internet-address (rest host-object)))
  2578.               ((:chaos :DECnet)
  2579.                (dpb (third host-object) (byte 8 8) (second host-object))))
  2580.             (net:local-network-of-type (if (eq family :DECnet)
  2581.                                :DNA
  2582.                                family)))
  2583.               :name)
  2584.         family)))
  2585.  
  2586.  
  2587. ;;-----------------------------------------------------------------------------
  2588. ;; Whether to use closures for requests or not.
  2589. ;;-----------------------------------------------------------------------------
  2590.  
  2591. ;;; If this macro expands to non-NIL, then request and locking code is
  2592. ;;; compiled in a much more compact format, as the common code is shared, and
  2593. ;;; the specific code is built into a closure that is funcalled by the shared
  2594. ;;; code.  If your compiler makes efficient use of closures then you probably
  2595. ;;; want to make this expand to T, as it makes the code more compact.
  2596.  
  2597. (defmacro use-closures ()
  2598.   #+(or lispm Minima) t
  2599.   #-(or lispm Minima) nil)
  2600.  
  2601.  
  2602. ;;-----------------------------------------------------------------------------
  2603. ;; Resource stuff
  2604. ;;-----------------------------------------------------------------------------
  2605.  
  2606.  
  2607. ;;; DEFAULT-RESOURCES-PATHNAME - The pathname of the resources file to load if
  2608. ;;; a resource manager isn't running.
  2609.  
  2610. (defun default-resources-pathname ()
  2611.   (when #+(or unix mach) t
  2612.         #-(or unix mach) (search "Unix" (software-type) :test #'char-equal)
  2613.     (merge-pathnames (user-homedir-pathname) (pathname ".Xdefaults"))))
  2614.  
  2615.  
  2616.  
  2617. ;;; RESOURCES-PATHNAME - The pathname of the resources file to load after the
  2618. ;;; defaults have been loaded.
  2619.  
  2620. (defun resources-pathname ()
  2621.   (when #+(or unix mach) t
  2622.     #-(or unix mach) (search "Unix" (software-type) :test #'char-equal)
  2623.     (or #+(or excl CLISP (and lcl3.0 (not vax-vms)) CMU)
  2624.     (let ((string #-CMU (#+excl sys:getenv #+CLISP system::getenv
  2625.                     #+lcl3.0 lcl:environment-variable
  2626.                     "XENVIRONMENT")
  2627.               #+CMU
  2628.               (cdr (assoc :xenvironment ext:*environment-list*))))
  2629.       (when string
  2630.         (pathname string)))
  2631.     (merge-pathnames
  2632.       (user-homedir-pathname)
  2633.       (pathname 
  2634.         (concatenate 'simple-string ".Xdefaults-"
  2635.              #+excl (short-site-name)
  2636.              #-excl (machine-instance)))))))
  2637.  
  2638.  
  2639. ;;-----------------------------------------------------------------------------
  2640. ;; GC stuff
  2641. ;;-----------------------------------------------------------------------------
  2642.  
  2643. (defun gc-cleanup ()
  2644.   (declare (special *event-free-list*
  2645.             *pending-command-free-list*
  2646.             *reply-buffer-free-lists*
  2647.             *gcontext-local-state-cache*
  2648.             *temp-gcontext-cache*))
  2649.   (setq *event-free-list* nil)
  2650.   (setq *pending-command-free-list* nil)
  2651.   (when (boundp '*reply-buffer-free-lists*)
  2652.     (fill *reply-buffer-free-lists* nil))
  2653.   (setq *gcontext-local-state-cache* nil)
  2654.   (setq *temp-gcontext-cache* nil)
  2655.   nil)
  2656.  
  2657. #+Genera
  2658. (si:define-gc-cleanup clx-cleanup ("CLX Cleanup")
  2659.   (gc-cleanup))
  2660.  
  2661.  
  2662. ;;-----------------------------------------------------------------------------
  2663. ;; WITH-STANDARD-IO-SYNTAX equivalent, used in (SETF WM-COMMAND)
  2664. ;;-----------------------------------------------------------------------------
  2665.  
  2666. #-(or clx-ansi-common-lisp Genera CMU)
  2667. (defun with-standard-io-syntax-function (function)
  2668.   (declare #+lispm
  2669.        (sys:downward-funarg function))
  2670.   (let ((*package* (find-package :user))
  2671.     (*print-array* t)
  2672.     (*print-base* 10)
  2673.     (*print-case* :upcase)
  2674.     (*print-circle* nil)
  2675.     (*print-escape* t)
  2676.     (*print-gensym* t)
  2677.     (*print-length* nil)
  2678.     (*print-level* nil)
  2679.     (*print-pretty* nil)
  2680.     (*print-radix* nil)
  2681.     (*read-base* 10)
  2682.     (*read-default-float-format* 'single-float)
  2683.     (*read-suppress* nil)
  2684.     #+ticl (ticl:*print-structure* t)
  2685.     #+lucid (lucid::*print-structure* t))
  2686.     (funcall function)))
  2687.  
  2688. #-(or clx-ansi-common-lisp Genera CMU)
  2689. (defmacro with-standard-io-syntax (&body body)
  2690.   `(flet ((.with-standard-io-syntax-body. () ,@body))
  2691.      (with-standard-io-syntax-function #'.with-standard-io-syntax-body.)))
  2692.  
  2693.  
  2694. ;;-----------------------------------------------------------------------------
  2695. ;; DEFAULT-KEYSYM-TRANSLATE
  2696. ;;-----------------------------------------------------------------------------
  2697.  
  2698. ;;; If object is a character, char-bits are set from state.
  2699. ;;;
  2700. ;;; [the following isn't implemented (should it be?)]
  2701. ;;; If object is a list, it is an alist with entries:
  2702. ;;; (base-char [modifiers] [mask-modifiers])
  2703. ;;; When MODIFIERS are specified, this character translation
  2704. ;;; will only take effect when the specified modifiers are pressed.
  2705. ;;; MASK-MODIFIERS can be used to specify a set of modifiers to ignore.
  2706. ;;; When MASK-MODIFIERS is missing, all other modifiers are ignored.
  2707. ;;; In ambiguous cases, the most specific translation is used.
  2708.  
  2709. #+(or (not (or cmu clx-ansi-common-lisp)) lispm allegro)
  2710. (defun default-keysym-translate (display state object)
  2711.   (declare (type display display)
  2712.        (type card16 state)
  2713.        (type t object)
  2714.        (values t)
  2715.        (special left-meta-keysym right-meta-keysym
  2716.             left-super-keysym right-super-keysym
  2717.             left-hyper-keysym right-hyper-keysym))
  2718.   (when (characterp object)
  2719.     (when (logbitp (position :control *state-mask-vector*) state)
  2720.       (setf (char-bit object :control) 1))
  2721.     (when (or (state-keysymp display state left-meta-keysym)
  2722.           (state-keysymp display state right-meta-keysym))
  2723.       (setf (char-bit object :meta) 1))
  2724.     (when (or (state-keysymp display state left-super-keysym)
  2725.           (state-keysymp display state right-super-keysym))
  2726.       (setf (char-bit object :super) 1))
  2727.     (when (or (state-keysymp display state left-hyper-keysym)
  2728.           (state-keysymp display state right-hyper-keysym))
  2729.       (setf (char-bit object :hyper) 1)))
  2730.   object)
  2731.  
  2732. #+(and (or cmu clx-ansi-common-lisp) (not lispm) (not allegro))
  2733. (defun default-keysym-translate (display state object)
  2734.   (declare (type display display)
  2735.        (type card16 state)
  2736.        (type t object)
  2737.        (ignore display state)
  2738.        (values t))
  2739.   object)
  2740.  
  2741.  
  2742. ;;-----------------------------------------------------------------------------
  2743. ;; Image stuff
  2744. ;;-----------------------------------------------------------------------------
  2745.  
  2746. ;;; Types
  2747.  
  2748. (deftype pixarray-1-element-type ()
  2749.   'bit)
  2750.  
  2751. (deftype pixarray-4-element-type ()
  2752.   '(unsigned-byte 4))
  2753.  
  2754. (deftype pixarray-8-element-type ()
  2755.   '(unsigned-byte 8))
  2756.  
  2757. (deftype pixarray-16-element-type ()
  2758.   '(unsigned-byte 16))
  2759.  
  2760. (deftype pixarray-24-element-type ()
  2761.   '(unsigned-byte 24))
  2762.  
  2763. (deftype pixarray-32-element-type ()
  2764.   #-(or Genera Minima) '(unsigned-byte 32)
  2765.   #+(or Genera Minima) 'fixnum)
  2766.  
  2767. (deftype pixarray-1  ()
  2768.   '(array pixarray-1-element-type (* *)))
  2769.  
  2770. (deftype pixarray-4  ()
  2771.   '(array pixarray-4-element-type (* *)))
  2772.  
  2773. (deftype pixarray-8  ()
  2774.   '(array pixarray-8-element-type (* *)))
  2775.  
  2776. (deftype pixarray-16 ()
  2777.   '(array pixarray-16-element-type (* *)))
  2778.  
  2779. (deftype pixarray-24 ()
  2780.   '(array pixarray-24-element-type (* *)))
  2781.  
  2782. (deftype pixarray-32 ()
  2783.   '(array pixarray-32-element-type (* *)))
  2784.  
  2785. (deftype pixarray ()
  2786.   '(or pixarray-1 pixarray-4 pixarray-8 pixarray-16 pixarray-24 pixarray-32))
  2787.  
  2788. (deftype bitmap ()
  2789.   'pixarray-1)
  2790.  
  2791. ;;; WITH-UNDERLYING-SIMPLE-VECTOR 
  2792.  
  2793. #+Genera
  2794. (defmacro with-underlying-simple-vector
  2795.       ((variable element-type pixarray) &body body)
  2796.   (let ((bits-per-element
  2797.       (sys:array-bits-per-element
  2798.         (symbol-value (sys:type-array-element-type element-type)))))
  2799.     `(scl:stack-let ((,variable
  2800.               (make-array
  2801.             (index-ceiling
  2802.               (index* (array-total-size ,pixarray)
  2803.                   (sys:array-element-size ,pixarray))
  2804.               ,bits-per-element)
  2805.             :element-type ',element-type
  2806.             :displaced-to ,pixarray)))
  2807.        (declare (array-register ,variable))
  2808.        ,@body)))
  2809.  
  2810. #+lcl3.0
  2811. (defmacro with-underlying-simple-vector
  2812.       ((variable element-type pixarray) &body body)
  2813.   `(let ((,variable (sys:underlying-simple-vector ,pixarray)))
  2814.      (declare (type (simple-array ,element-type (*)) ,variable))
  2815.      ,@body))
  2816.  
  2817. #+excl
  2818. (defmacro with-underlying-simple-vector
  2819.       ((variable element-type pixarray) &body body)
  2820.   `(let ((,variable (cdr (excl::ah_data ,pixarray))))
  2821.      (declare (type (simple-array ,element-type (*)) ,variable))
  2822.      ,@body))
  2823.  
  2824. #+CMU
  2825. ;;; We do *NOT* support viewing an array as having a different element type.
  2826. ;;; Element-type is ignored.
  2827. ;;;
  2828. (defmacro with-underlying-simple-vector 
  2829.       ((variable element-type pixarray) &body body)
  2830.   (declare (ignore element-type))
  2831.   `(lisp::with-array-data ((,variable ,pixarray)
  2832.                (start)
  2833.                (end))
  2834.       (declare (ignore start end))
  2835.       ,@body))
  2836.  
  2837. ;;; These are used to read and write pixels from and to CARD8s.
  2838.  
  2839. ;;; READ-IMAGE-LOAD-BYTE is used to extract 1 and 4 bit pixels from CARD8s.
  2840.  
  2841. (defmacro read-image-load-byte (size position integer)
  2842.   (unless *image-bit-lsb-first-p* (setq position (- 7 position)))
  2843.   `(the (unsigned-byte ,size)
  2844.     (#-Genera ldb #+Genera sys:%logldb
  2845.      (byte ,size ,position)
  2846.      (the card8 ,integer))))
  2847.  
  2848. ;;; READ-IMAGE-ASSEMBLE-BYTES is used to build 16, 24 and 32 bit pixels from
  2849. ;;; the appropriate number of CARD8s.
  2850.  
  2851. (defmacro read-image-assemble-bytes (&rest bytes)
  2852.   (unless *image-byte-lsb-first-p* (setq bytes (reverse bytes)))
  2853.   (let ((it (first bytes))
  2854.     (count 0))
  2855.     (dolist (byte (rest bytes))
  2856.       (setq it
  2857.         `(#-Genera dpb #+Genera sys:%logdpb 
  2858.           (the card8 ,byte)
  2859.           (byte 8 ,(incf count 8))
  2860.           (the (unsigned-byte ,count) ,it))))
  2861.     #-Genera `(the (unsigned-byte ,(* (length bytes) 8)) ,it)
  2862.     #+Genera it))
  2863.  
  2864. ;;; WRITE-IMAGE-LOAD-BYTE is used to extract a CARD8 from a 16, 24 or 32 bit
  2865. ;;; pixel.
  2866.  
  2867. (defmacro write-image-load-byte (position integer integer-size)
  2868.   integer-size
  2869.   (unless *image-byte-lsb-first-p* (setq position (- integer-size 8 position)))
  2870.   `(the card8
  2871.     (#-Genera ldb #+Genera sys:%logldb
  2872.      (byte 8 ,position)
  2873.      #-Genera (the (unsigned-byte ,integer-size) ,integer)
  2874.      #+Genera ,integer
  2875.      )))
  2876.  
  2877. ;;; WRITE-IMAGE-ASSEMBLE-BYTES is used to build a CARD8 from 1 or 4 bit
  2878. ;;; pixels.
  2879.  
  2880. (defmacro write-image-assemble-bytes (&rest bytes)
  2881.   (unless *image-bit-lsb-first-p* (setq bytes (reverse bytes)))
  2882.   (let ((size (floor 8 (length bytes)))
  2883.     (it (first bytes))
  2884.     (count 0))
  2885.     (dolist (byte (rest bytes))
  2886.       (setq it `(#-Genera dpb #+Genera sys:%logdpb
  2887.          (the (unsigned-byte ,size) ,byte)
  2888.          (byte ,size ,(incf count size))
  2889.          (the (unsigned-byte ,count) ,it))))
  2890.     `(the card8 ,it)))
  2891.  
  2892. #+(or Genera lcl3.0 excl)
  2893. (defvar *computed-image-byte-lsb-first-p* *image-byte-lsb-first-p*)
  2894.  
  2895. #+(or Genera lcl3.0 excl)
  2896. (defvar *computed-image-bit-lsb-first-p* *image-bit-lsb-first-p*)
  2897.  
  2898. ;;; The following table gives the bit ordering within bytes (when accessed
  2899. ;;; sequentially) for a scanline containing 32 bits, with bits numbered 0 to
  2900. ;;; 31, where bit 0 should be leftmost on the display.  For a given byte
  2901. ;;; labelled A-B, A is for the most significant bit of the byte, and B is
  2902. ;;; for the least significant bit.
  2903. ;;; 
  2904. ;;; legend:
  2905. ;;;     1   scanline-unit = 8
  2906. ;;;     2   scanline-unit = 16
  2907. ;;;     4   scanline-unit = 32
  2908. ;;;     M   byte-order = MostSignificant
  2909. ;;;     L   byte-order = LeastSignificant
  2910. ;;;     m   bit-order = MostSignificant
  2911. ;;;     l   bit-order = LeastSignificant
  2912. ;;; 
  2913. ;;; 
  2914. ;;; format    ordering
  2915. ;;; 
  2916. ;;; 1Mm    00-07 08-15 16-23 24-31
  2917. ;;; 2Mm    00-07 08-15 16-23 24-31
  2918. ;;; 4Mm    00-07 08-15 16-23 24-31
  2919. ;;; 1Ml    07-00 15-08 23-16 31-24
  2920. ;;; 2Ml    15-08 07-00 31-24 23-16
  2921. ;;; 4Ml    31-24 23-16 15-08 07-00
  2922. ;;; 1Lm    00-07 08-15 16-23 24-31
  2923. ;;; 2Lm    08-15 00-07 24-31 16-23
  2924. ;;; 4Lm    24-31 16-23 08-15 00-07
  2925. ;;; 1Ll    07-00 15-08 23-16 31-24
  2926. ;;; 2Ll    07-00 15-08 23-16 31-24
  2927. ;;; 4Ll    07-00 15-08 23-16 31-24
  2928.  
  2929. #+(or Genera lcl3.0 excl) 
  2930. (defconstant
  2931.   *image-bit-ordering-table*
  2932.   '(((1 (00 07) (08 15) (16 23) (24 31)) (nil nil))
  2933.     ((2 (00 07) (08 15) (16 23) (24 31)) (nil nil))
  2934.     ((4 (00 07) (08 15) (16 23) (24 31)) (nil nil))
  2935.     ((1 (07 00) (15 08) (23 16) (31 24)) (nil t))
  2936.     ((2 (15 08) (07 00) (31 24) (23 16)) (nil t))
  2937.     ((4 (31 24) (23 16) (15 08) (07 00)) (nil t))
  2938.     ((1 (00 07) (08 15) (16 23) (24 31)) (t   nil))
  2939.     ((2 (08 15) (00 07) (24 31) (16 23)) (t   nil))
  2940.     ((4 (24 31) (16 23) (08 15) (00 07)) (t   nil))
  2941.     ((1 (07 00) (15 08) (23 16) (31 24)) (t   t))
  2942.     ((2 (07 00) (15 08) (23 16) (31 24)) (t   t))
  2943.     ((4 (07 00) (15 08) (23 16) (31 24)) (t   t))))
  2944.   
  2945. #+(or Genera lcl3.0 excl) 
  2946. (defun compute-image-byte-and-bit-ordering ()
  2947.   (declare (values image-byte-lsb-first-p image-bit-lsb-first-p))
  2948.   ;; First compute the ordering 
  2949.   (let ((ordering nil)
  2950.     (a (make-array '(1 32) :element-type 'bit :initial-element 0)))
  2951.     (dotimes (i 4)
  2952.       (push (flet ((bitpos (a i n)
  2953.              (declare (optimize (speed 3) (safety 0) (space 0)))
  2954.              (declare (type (simple-array bit (* *)) a)
  2955.                   (type fixnum i n))
  2956.              (with-underlying-simple-vector (v (unsigned-byte 8) a)
  2957.                (prog2
  2958.              (setf (aref v i) n)
  2959.              (dotimes (i 32)
  2960.                (unless (zerop (aref a 0 i))
  2961.                  (return i)))
  2962.              (setf (aref v i) 0)))))
  2963.           (list (bitpos a i #b10000000)
  2964.             (bitpos a i #b00000001)))
  2965.         ordering))
  2966.     (setq ordering (cons (floor *image-unit* 8) (nreverse ordering)))
  2967.     ;; Now from the ordering, compute byte-lsb-first-p and bit-lsb-first-p
  2968.     (let ((byte-and-bit-ordering
  2969.         (second (assoc ordering *image-bit-ordering-table*
  2970.                :test #'equal))))
  2971.       (unless byte-and-bit-ordering
  2972.     (error "Couldn't determine image byte and bit ordering~@
  2973.                 measured image ordering = ~A"
  2974.            ordering))
  2975.       (values-list byte-and-bit-ordering))))
  2976.  
  2977. #+(or Genera lcl3.0 excl) 
  2978. (multiple-value-setq
  2979.   (*computed-image-byte-lsb-first-p* *computed-image-bit-lsb-first-p*)
  2980.   (compute-image-byte-and-bit-ordering))
  2981.  
  2982. ;;; If you can write fast routines that can read and write pixarrays out of a
  2983. ;;; buffer-bytes, do it!  It makes the image code a lot faster.  The
  2984. ;;; FAST-READ-PIXARRAY, FAST-WRITE-PIXARRAY and FAST-COPY-PIXARRAY routines
  2985. ;;; return T if they can do it, NIL if they can't.
  2986.  
  2987. ;;; FAST-READ-PIXARRAY - fill part of a pixarray from a buffer of card8s
  2988.  
  2989. #+(or lcl3.0 excl)
  2990. (defun fast-read-pixarray-1 (buffer-bbuf index array x y width height  
  2991.                  padded-bytes-per-line bits-per-pixel)
  2992.   (declare (type buffer-bytes buffer-bbuf)
  2993.        (type pixarray-1 array)
  2994.        (type card16 x y width height)
  2995.        (type array-index index padded-bytes-per-line)
  2996.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  2997.        (ignore bits-per-pixel))
  2998.   #.(declare-buffun)
  2999.   (with-vector (buffer-bbuf buffer-bytes)
  3000.     (with-underlying-simple-vector (vector pixarray-1-element-type array)
  3001.       (do* ((start (index+ index
  3002.                (index* y padded-bytes-per-line)
  3003.                (index-ceiling x 8))
  3004.            (index+ start padded-bytes-per-line))
  3005.         (y 0 (index1+ y))
  3006.         (left-bits (the array-index (mod (the fixnum (- x)) 8)))
  3007.         (right-bits (index-mod (index- width left-bits) 8))
  3008.         (middle-bits (the fixnum (- (the fixnum (- width left-bits))
  3009.                     right-bits)))
  3010.         (middle-bytes (index-floor middle-bits 8)))
  3011.        ((index>= y height))
  3012.     (declare (type array-index start y
  3013.                left-bits right-bits middle-bytes)
  3014.          (fixnum middle-bits))
  3015.     (cond ((< middle-bits 0)
  3016.            (let ((byte (aref buffer-bbuf (index1- start)))
  3017.              (x (array-row-major-index array y left-bits)))
  3018.          (declare (type card8 byte)
  3019.               (type array-index x))
  3020.          (when (index> right-bits 6)
  3021.            (setf (aref vector (index- x 1))
  3022.              (read-image-load-byte 1 7 byte)))
  3023.          (when (and (index> left-bits 1)
  3024.                 (index> right-bits 5))
  3025.            (setf (aref vector (index- x 2))
  3026.              (read-image-load-byte 1 6 byte)))
  3027.          (when (and (index> left-bits 2)
  3028.                 (index> right-bits 4))
  3029.            (setf (aref vector (index- x 3))
  3030.              (read-image-load-byte 1 5 byte)))
  3031.          (when (and (index> left-bits 3)
  3032.                 (index> right-bits 3))
  3033.            (setf (aref vector (index- x 4))
  3034.              (read-image-load-byte 1 4 byte)))
  3035.          (when (and (index> left-bits 4)
  3036.                 (index> right-bits 2))
  3037.            (setf (aref vector (index- x 5))
  3038.              (read-image-load-byte 1 3 byte)))
  3039.          (when (and (index> left-bits 5)
  3040.                 (index> right-bits 1))
  3041.            (setf (aref vector (index- x 6))
  3042.              (read-image-load-byte 1 2 byte)))
  3043.          (when (index> left-bits 6)
  3044.            (setf (aref vector (index- x 7))
  3045.              (read-image-load-byte 1 1 byte)))))
  3046.           (t
  3047.            (unless (index-zerop left-bits)
  3048.          (let ((byte (aref buffer-bbuf (index1- start)))
  3049.                (x (array-row-major-index array y left-bits)))
  3050.            (declare (type card8 byte)
  3051.                 (type array-index x))
  3052.            (setf (aref vector (index- x 1))
  3053.              (read-image-load-byte 1 7 byte))
  3054.            (when (index> left-bits 1)
  3055.              (setf (aref vector (index- x 2))
  3056.                (read-image-load-byte 1 6 byte))
  3057.              (when (index> left-bits 2)
  3058.                (setf (aref vector (index- x 3))
  3059.                  (read-image-load-byte 1 5 byte))
  3060.                (when (index> left-bits 3)
  3061.              (setf (aref vector (index- x 4))
  3062.                    (read-image-load-byte 1 4 byte))
  3063.              (when (index> left-bits 4)
  3064.                (setf (aref vector (index- x 5))
  3065.                  (read-image-load-byte 1 3 byte))
  3066.                (when (index> left-bits 5)
  3067.                  (setf (aref vector (index- x 6))
  3068.                    (read-image-load-byte 1 2 byte))
  3069.                  (when (index> left-bits 6)
  3070.                    (setf (aref vector (index- x 7))
  3071.                      (read-image-load-byte 1 1 byte))
  3072.                    ))))))))
  3073.            (do* ((end (index+ start middle-bytes))
  3074.              (i start (index1+ i))
  3075.              (x (array-row-major-index array y left-bits) (index+ x 8)))
  3076.             ((index>= i end)
  3077.              (unless (index-zerop right-bits)
  3078.                (let ((byte (aref buffer-bbuf end))
  3079.                  (x (array-row-major-index
  3080.                  array y (index+ left-bits middle-bits))))
  3081.              (declare (type card8 byte)
  3082.                   (type array-index x))
  3083.              (setf (aref vector (index+ x 0))
  3084.                    (read-image-load-byte 1 0 byte))
  3085.              (when (index> right-bits 1)
  3086.                (setf (aref vector (index+ x 1))
  3087.                  (read-image-load-byte 1 1 byte))
  3088.                (when (index> right-bits 2)
  3089.                  (setf (aref vector (index+ x 2))
  3090.                    (read-image-load-byte 1 2 byte))
  3091.                  (when (index> right-bits 3)
  3092.                    (setf (aref vector (index+ x 3))
  3093.                      (read-image-load-byte 1 3 byte))
  3094.                    (when (index> right-bits 4)
  3095.                  (setf (aref vector (index+ x 4))
  3096.                        (read-image-load-byte 1 4 byte))
  3097.                  (when (index> right-bits 5)
  3098.                    (setf (aref vector (index+ x 5))
  3099.                      (read-image-load-byte 1 5 byte))
  3100.                    (when (index> right-bits 6)
  3101.                      (setf (aref vector (index+ x 6))
  3102.                        (read-image-load-byte 1 6 byte))
  3103.                      )))))))))
  3104.          (declare (type array-index end i x))
  3105.          (let ((byte (aref buffer-bbuf i)))
  3106.            (declare (type card8 byte))
  3107.            (setf (aref vector (index+ x 0))
  3108.              (read-image-load-byte 1 0 byte))
  3109.            (setf (aref vector (index+ x 1))
  3110.              (read-image-load-byte 1 1 byte))
  3111.            (setf (aref vector (index+ x 2))
  3112.              (read-image-load-byte 1 2 byte))
  3113.            (setf (aref vector (index+ x 3))
  3114.              (read-image-load-byte 1 3 byte))
  3115.            (setf (aref vector (index+ x 4))
  3116.              (read-image-load-byte 1 4 byte))
  3117.            (setf (aref vector (index+ x 5))
  3118.              (read-image-load-byte 1 5 byte))
  3119.            (setf (aref vector (index+ x 6))
  3120.              (read-image-load-byte 1 6 byte))
  3121.            (setf (aref vector (index+ x 7))
  3122.              (read-image-load-byte 1 7 byte))))
  3123.            )))))
  3124.     t)
  3125.  
  3126. #+(or lcl3.0 excl)
  3127. (defun fast-read-pixarray-4 (buffer-bbuf index array x y width height 
  3128.                  padded-bytes-per-line bits-per-pixel)
  3129.   (declare (type buffer-bytes buffer-bbuf)
  3130.        (type pixarray-4 array)
  3131.        (type card16 x y width height)
  3132.        (type array-index index padded-bytes-per-line)
  3133.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3134.        (ignore bits-per-pixel))
  3135.   #.(declare-buffun)
  3136.   (with-vector (buffer-bbuf buffer-bytes)
  3137.     (with-underlying-simple-vector (vector pixarray-4-element-type array)
  3138.       (do* ((start (index+ index
  3139.                (index* y padded-bytes-per-line)
  3140.                (index-ceiling x 2))
  3141.            (index+ start padded-bytes-per-line))
  3142.         (y 0 (index1+ y))
  3143.         (left-nibbles (the array-index (mod (the fixnum (- (the fixnum x)))
  3144.                         2)))
  3145.         (right-nibbles (index-mod (index- width left-nibbles) 2))
  3146.         (middle-nibbles (index- width left-nibbles right-nibbles))
  3147.         (middle-bytes (index-floor middle-nibbles 2)))
  3148.        ((index>= y height))
  3149.     (declare (type array-index start y
  3150.                left-nibbles right-nibbles middle-nibbles middle-bytes))
  3151.     (unless (index-zerop left-nibbles)
  3152.       (setf (aref array y 0)
  3153.         (read-image-load-byte
  3154.           4 4 (aref buffer-bbuf (index1- start)))))
  3155.     (do* ((end (index+ start middle-bytes))
  3156.           (i start (index1+ i))
  3157.           (x (array-row-major-index array y left-nibbles) (index+ x 2)))
  3158.          ((index>= i end)
  3159.           (unless (index-zerop right-nibbles)
  3160.         (setf (aref array y (index+ left-nibbles middle-nibbles))
  3161.               (read-image-load-byte 4 0 (aref buffer-bbuf end)))))
  3162.       (declare (type array-index end i x))
  3163.       (let ((byte (aref buffer-bbuf i)))
  3164.         (declare (type card8 byte))
  3165.         (setf (aref vector (index+ x 0))
  3166.           (read-image-load-byte 4 0 byte))
  3167.         (setf (aref vector (index+ x 1))
  3168.           (read-image-load-byte 4 4 byte))))
  3169.     )))
  3170.   t)
  3171.  
  3172. #+(or Genera lcl3.0 excl CMU)
  3173. (defun fast-read-pixarray-24 (buffer-bbuf index array x y width height 
  3174.                   padded-bytes-per-line bits-per-pixel)
  3175.   (declare (type buffer-bytes buffer-bbuf)
  3176.        (type pixarray-24 array)
  3177.        (type card16 width height)
  3178.        (type array-index index padded-bytes-per-line)
  3179.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3180.        (ignore bits-per-pixel))
  3181.   #.(declare-buffun)
  3182.   (with-vector (buffer-bbuf buffer-bytes)
  3183.     (with-underlying-simple-vector (vector pixarray-24-element-type array)
  3184.       (do* ((start (index+ index
  3185.                (index* y padded-bytes-per-line)
  3186.                (index* x 3))
  3187.            (index+ start padded-bytes-per-line))
  3188.         (y 0 (index1+ y)))
  3189.        ((index>= y height))
  3190.     (declare (type array-index start y))
  3191.     (do* ((end (index+ start (index* width 3)))
  3192.           (i start (index+ i 3))
  3193.           (x (array-row-major-index array y 0) (index1+ x)))
  3194.          ((index>= i end))
  3195.       (declare (type array-index end i x))
  3196.       (setf (aref vector x)
  3197.         (read-image-assemble-bytes
  3198.           (aref buffer-bbuf (index+ i 0))
  3199.           (aref buffer-bbuf (index+ i 1))
  3200.           (aref buffer-bbuf (index+ i 2))))))))
  3201.   t)
  3202.  
  3203. #+lispm
  3204. (defun fast-read-pixarray-using-bitblt
  3205.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  3206.     bits-per-pixel)
  3207.   (#+Genera sys:stack-let* #-Genera let*
  3208.    ((dimensions (list (+ y height)
  3209.               (floor (* padded-bytes-per-line 8) bits-per-pixel)))
  3210.     (a (make-array
  3211.      dimensions
  3212.      :element-type (array-element-type pixarray)
  3213.      :displaced-to bbuf
  3214.      :displaced-index-offset (floor (* boffset 8) bits-per-pixel))))
  3215.    (sys:bitblt boole-1 width height a x y pixarray 0 0))
  3216.   t)
  3217.  
  3218. #+CMU
  3219. (defun pixarray-element-size (pixarray)
  3220.   (let ((eltype (array-element-type pixarray)))
  3221.     (cond ((eq eltype 'bit) 1)
  3222.       ((and (consp eltype) (eq (first eltype) 'unsigned-byte))
  3223.        (second eltype))
  3224.       (t
  3225.        (error "Invalid pixarray: ~S." pixarray)))))
  3226.  
  3227. #+CMU
  3228. ;;; COPY-BIT-RECT  --  Internal
  3229. ;;;
  3230. ;;;    This is the classic BITBLT operation, copying a rectangular subarray
  3231. ;;; from one array to another (but source and destination must not overlap.)
  3232. ;;; Widths are specified in bits.  Neither array can have a non-zero
  3233. ;;; displacement.  We allow extra random bit-offset to be thrown into the X.
  3234. ;;;
  3235. (defun copy-bit-rect (source source-width sx sy dest dest-width dx dy
  3236.                  height width)
  3237.   (declare (type array-index source-width sx sy dest-width dx dy height width))
  3238.    #.(declare-buffun)
  3239.    (lisp::with-array-data ((sdata source)
  3240.                (sstart)
  3241.                (send))
  3242.      (declare (ignore send))
  3243.      (lisp::with-array-data ((ddata dest)
  3244.                  (dstart)
  3245.                  (dend))
  3246.        (declare (ignore dend))
  3247.        (assert (and (zerop sstart) (zerop dstart)))
  3248.        (do ((src-idx (index+ (* vm:vector-data-offset vm:word-bits)
  3249.                  sx (index* sy source-width))
  3250.              (index+ src-idx source-width))
  3251.         (dest-idx (index+ (* vm:vector-data-offset vm:word-bits)
  3252.                   dx (index* dy dest-width))
  3253.               (index+ dest-idx dest-width))
  3254.         (count height (1- count)))
  3255.        ((zerop count))
  3256.      (declare (type array-index src-idx dest-idx count))
  3257.      (kernel:bit-bash-copy sdata src-idx ddata dest-idx width)))))
  3258.  
  3259. #+CMU
  3260. (defun fast-read-pixarray-using-bitblt
  3261.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  3262.     bits-per-pixel)
  3263.   (declare (type (array * 2) pixarray))
  3264.   #.(declare-buffun)
  3265.   (copy-bit-rect bbuf
  3266.          (index* padded-bytes-per-line vm:byte-bits)
  3267.          (index* boffset vm:byte-bits) 0
  3268.          pixarray
  3269.          (index* (array-dimension pixarray 1) bits-per-pixel)
  3270.          x y
  3271.          height width)
  3272.   t)
  3273.  
  3274. #+(or Genera lcl3.0 excl)
  3275. (defun fast-read-pixarray-with-swap
  3276.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  3277.     bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  3278.   (declare (type buffer-bytes bbuf)
  3279.        (type array-index boffset
  3280.          padded-bytes-per-line)
  3281.        (type pixarray pixarray)
  3282.        (type card16 x y width height)
  3283.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3284.        (type (member 8 16 32) unit)
  3285.        (type boolean byte-lsb-first-p bit-lsb-first-p))
  3286.   (unless (index= bits-per-pixel 24)
  3287.     (let ((pixarray-padded-bits-per-line
  3288.         (if (index= height 1) 0
  3289.           (index* (index- (array-row-major-index pixarray 1 0)
  3290.                   (array-row-major-index pixarray 0 0))
  3291.               bits-per-pixel)))
  3292.       (x-bits (index* x bits-per-pixel)))
  3293.       (declare (type array-index pixarray-padded-bits-per-line x-bits))
  3294.       (when (if (eq *computed-image-byte-lsb-first-p* *computed-image-bit-lsb-first-p*)
  3295.         (and (index-zerop (index-mod pixarray-padded-bits-per-line 8))
  3296.              (index-zerop (index-mod x-bits 8)))
  3297.           (and (index-zerop (index-mod pixarray-padded-bits-per-line *image-unit*))
  3298.            (index-zerop (index-mod x-bits *image-unit*))))
  3299.     (multiple-value-bind (image-swap-function image-swap-lsb-first-p)
  3300.         (image-swap-function
  3301.           bits-per-pixel 
  3302.           unit byte-lsb-first-p bit-lsb-first-p
  3303.           *image-unit* *computed-image-byte-lsb-first-p*
  3304.           *computed-image-bit-lsb-first-p*)
  3305.       (declare (type symbol image-swap-function)
  3306.            (type boolean image-swap-lsb-first-p))
  3307.       (with-underlying-simple-vector (dst card8 pixarray)
  3308.         (funcall
  3309.           (symbol-function image-swap-function) bbuf dst
  3310.           (index+ boffset
  3311.               (index* y padded-bytes-per-line)
  3312.               (index-floor x-bits 8))
  3313.           0 (index-ceiling (index* width bits-per-pixel) 8)
  3314.           padded-bytes-per-line
  3315.           (index-floor pixarray-padded-bits-per-line 8)
  3316.           height image-swap-lsb-first-p)))
  3317.     t))))
  3318.  
  3319. (defun fast-read-pixarray (bbuf boffset pixarray
  3320.                x y width height padded-bytes-per-line
  3321.                bits-per-pixel
  3322.                unit byte-lsb-first-p bit-lsb-first-p)
  3323.   (declare (type buffer-bytes bbuf)
  3324.        (type array-index boffset
  3325.          padded-bytes-per-line)
  3326.        (type pixarray pixarray)
  3327.        (type card16 x y width height)
  3328.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3329.        (type (member 8 16 32) unit)
  3330.        (type boolean byte-lsb-first-p bit-lsb-first-p))
  3331.   (progn bbuf boffset pixarray x y width height padded-bytes-per-line
  3332.      bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  3333.   (or
  3334.     #+(or Genera lcl3.0 excl)
  3335.     (fast-read-pixarray-with-swap
  3336.       bbuf boffset pixarray x y width height padded-bytes-per-line
  3337.       bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  3338.     (let ((function
  3339.         (or #+lispm
  3340.         (and (= (sys:array-element-size pixarray) bits-per-pixel)
  3341.              (zerop (index-mod padded-bytes-per-line 4))
  3342.              (zerop (index-mod
  3343.                   (* #+Genera (sys:array-row-span pixarray)
  3344.                  #-Genera (array-dimension pixarray 1)
  3345.                  bits-per-pixel)
  3346.                   32))
  3347.              #'fast-read-pixarray-using-bitblt)
  3348.         #+CMU
  3349.         (and (index= (pixarray-element-size pixarray) bits-per-pixel)
  3350.              #'fast-read-pixarray-using-bitblt)
  3351.         #+(or lcl3.0 excl)
  3352.         (and (index= bits-per-pixel 1)
  3353.              #'fast-read-pixarray-1)
  3354.         #+(or lcl3.0 excl)
  3355.         (and (index= bits-per-pixel 4)
  3356.              #'fast-read-pixarray-4)
  3357.         #+(or Genera lcl3.0 excl CMU)
  3358.         (and (index= bits-per-pixel 24)
  3359.              #'fast-read-pixarray-24))))
  3360.       (when function
  3361.     (read-pixarray-internal
  3362.       bbuf boffset pixarray x y width height padded-bytes-per-line
  3363.       bits-per-pixel function
  3364.       unit byte-lsb-first-p bit-lsb-first-p
  3365.       *image-unit* *image-byte-lsb-first-p* *image-bit-lsb-first-p*)))))
  3366.  
  3367. ;;; FAST-WRITE-PIXARRAY - copy part of a pixarray into an array of CARD8s
  3368.  
  3369. #+(or lcl3.0 excl)
  3370. (defun fast-write-pixarray-1 (buffer-bbuf index array x y width height
  3371.                   padded-bytes-per-line bits-per-pixel)
  3372.   (declare (type buffer-bytes buffer-bbuf)
  3373.        (type pixarray-1 array)
  3374.        (type card16 x y width height)
  3375.        (type array-index index padded-bytes-per-line)
  3376.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3377.        (ignore bits-per-pixel))
  3378.   #.(declare-buffun)
  3379.   (with-vector (buffer-bbuf buffer-bytes)
  3380.     (with-underlying-simple-vector (vector pixarray-1-element-type array)
  3381.       (do* ((h 0 (index1+ h))
  3382.         (y y (index1+ y))
  3383.         (right-bits (index-mod width 8))
  3384.         (middle-bits (index- width right-bits))
  3385.         (middle-bytes (index-ceiling middle-bits 8))
  3386.         (start index (index+ start padded-bytes-per-line)))
  3387.        ((index>= h height))
  3388.     (declare (type array-index h y right-bits middle-bits
  3389.                middle-bytes start))
  3390.     (do* ((end (index+ start middle-bytes))
  3391.           (i start (index1+ i))
  3392.           (start-x x)
  3393.           (x (array-row-major-index array y start-x) (index+ x 8)))
  3394.          ((index>= i end)
  3395.           (unless (index-zerop right-bits)
  3396.         (let ((x (array-row-major-index
  3397.                array y (index+ start-x middle-bits))))
  3398.           (declare (type array-index x))
  3399.           (setf (aref buffer-bbuf end)
  3400.             (write-image-assemble-bytes
  3401.               (aref vector (index+ x 0))
  3402.               (if (index> right-bits 1)
  3403.                   (aref vector (index+ x 1))
  3404.                 0)
  3405.               (if (index> right-bits 2)
  3406.                   (aref vector (index+ x 2))
  3407.                 0)
  3408.               (if (index> right-bits 3)
  3409.                   (aref vector (index+ x 3))
  3410.                 0)
  3411.               (if (index> right-bits 4)
  3412.                   (aref vector (index+ x 4))
  3413.                 0)
  3414.               (if (index> right-bits 5)
  3415.                   (aref vector (index+ x 5))
  3416.                 0)
  3417.               (if (index> right-bits 6)
  3418.                   (aref vector (index+ x 6))
  3419.                 0)
  3420.               0)))))
  3421.       (declare (type array-index end i start-x x))
  3422.       (setf (aref buffer-bbuf i)
  3423.         (write-image-assemble-bytes
  3424.           (aref vector (index+ x 0))
  3425.           (aref vector (index+ x 1))
  3426.           (aref vector (index+ x 2))
  3427.           (aref vector (index+ x 3))
  3428.           (aref vector (index+ x 4))
  3429.           (aref vector (index+ x 5))
  3430.           (aref vector (index+ x 6))
  3431.           (aref vector (index+ x 7))))))))
  3432.   t)
  3433.  
  3434. #+(or lcl3.0 excl)
  3435. (defun fast-write-pixarray-4 (buffer-bbuf index array x y width height
  3436.                   padded-bytes-per-line bits-per-pixel)
  3437.   (declare (type buffer-bytes buffer-bbuf)
  3438.        (type pixarray-4 array)
  3439.        (type int16 x y)
  3440.        (type card16 width height)
  3441.        (type array-index index padded-bytes-per-line)
  3442.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3443.        (ignore bits-per-pixel))
  3444.   #.(declare-buffun)
  3445.   (with-vector (buffer-bbuf buffer-bytes)
  3446.     (with-underlying-simple-vector (vector pixarray-4-element-type array)
  3447.       (do* ((h 0 (index1+ h))
  3448.         (y y (index1+ y))
  3449.         (right-nibbles (index-mod width 2))
  3450.         (middle-nibbles (index- width right-nibbles))
  3451.         (middle-bytes (index-ceiling middle-nibbles 2))
  3452.         (start index (index+ start padded-bytes-per-line)))
  3453.        ((index>= h height))
  3454.     (declare (type array-index h y right-nibbles middle-nibbles
  3455.                middle-bytes start))
  3456.     (do* ((end (index+ start middle-bytes))
  3457.           (i start (index1+ i))
  3458.           (start-x x)
  3459.           (x (array-row-major-index array y start-x) (index+ x 2)))
  3460.          ((index>= i end)
  3461.           (unless (index-zerop right-nibbles)
  3462.         (setf (aref buffer-bbuf end)
  3463.               (write-image-assemble-bytes
  3464.             (aref array y (index+ start-x middle-nibbles))
  3465.             0))))
  3466.       (declare (type array-index end i start-x x))
  3467.       (setf (aref buffer-bbuf i)
  3468.         (write-image-assemble-bytes
  3469.           (aref vector (index+ x 0))
  3470.           (aref vector (index+ x 1))))))))
  3471.   t)
  3472.  
  3473. #+(or Genera lcl3.0 excl CMU)
  3474. (defun fast-write-pixarray-24 (buffer-bbuf index array x y width height
  3475.                    padded-bytes-per-line bits-per-pixel)
  3476.   (declare (type buffer-bytes buffer-bbuf)
  3477.        (type pixarray-24 array)
  3478.        (type int16 x y)
  3479.        (type card16 width height)
  3480.        (type array-index index padded-bytes-per-line)
  3481.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3482.        (ignore bits-per-pixel))
  3483.   #.(declare-buffun)
  3484.   (with-vector (buffer-bbuf buffer-bytes)
  3485.     (with-underlying-simple-vector (vector pixarray-24-element-type array)
  3486.       (do* ((h 0 (index1+ h))
  3487.         (y y (index1+ y))
  3488.         (start index (index+ start padded-bytes-per-line)))
  3489.        ((index>= h height))
  3490.     (declare (type array-index y start))
  3491.     (do* ((end (index+ start (index* width 3)))
  3492.           (i start (index+ i 3))
  3493.           (x (array-row-major-index array y x) (index1+ x)))
  3494.          ((index>= i end))
  3495.       (declare (type array-index end i x))
  3496.       (let ((pixel (aref vector x)))
  3497.         (declare (type pixarray-24-element-type pixel))
  3498.         (setf (aref buffer-bbuf (index+ i 0))
  3499.           (write-image-load-byte 0 pixel 24))
  3500.         (setf (aref buffer-bbuf (index+ i 1))
  3501.           (write-image-load-byte 8 pixel 24))
  3502.         (setf (aref buffer-bbuf (index+ i 2))
  3503.           (write-image-load-byte 16 pixel 24)))))))
  3504.   t)
  3505.  
  3506. #+lispm
  3507. (defun fast-write-pixarray-using-bitblt
  3508.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  3509.     bits-per-pixel)
  3510.   (#+Genera sys:stack-let* #-Genera let*
  3511.    ((dimensions (list (+ y height)
  3512.               (floor (* padded-bytes-per-line 8) bits-per-pixel)))
  3513.     (a (make-array
  3514.      dimensions
  3515.      :element-type (array-element-type pixarray)
  3516.      :displaced-to bbuf
  3517.      :displaced-index-offset (floor (* boffset 8) bits-per-pixel))))
  3518.    (sys:bitblt boole-1 width height pixarray x y a 0 0))
  3519.   t)
  3520.  
  3521. #+CMU
  3522. (defun fast-write-pixarray-using-bitblt
  3523.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  3524.     bits-per-pixel)
  3525.   #.(declare-buffun)
  3526.   (copy-bit-rect pixarray
  3527.          (index* (array-dimension pixarray 1) bits-per-pixel)
  3528.          x y
  3529.          bbuf
  3530.          (index* padded-bytes-per-line vm:byte-bits)
  3531.          (index* boffset vm:byte-bits) 0
  3532.          height width)
  3533.   t)
  3534.  
  3535. #+(or Genera lcl3.0 excl)
  3536. (defun fast-write-pixarray-with-swap
  3537.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  3538.     bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  3539.   (declare (type buffer-bytes bbuf)
  3540.        (type pixarray pixarray)
  3541.        (type card16 x y width height)
  3542.        (type array-index boffset padded-bytes-per-line)
  3543.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3544.        (type (member 8 16 32) unit)
  3545.        (type boolean byte-lsb-first-p bit-lsb-first-p))
  3546.   (unless (index= bits-per-pixel 24)
  3547.     (let ((pixarray-padded-bits-per-line
  3548.         (if (index= height 1) 0
  3549.           (index* (index- (array-row-major-index pixarray 1 0)
  3550.                   (array-row-major-index pixarray 0 0))
  3551.               bits-per-pixel)))
  3552.       (pixarray-start-bit-offset
  3553.         (index* (array-row-major-index pixarray y x)
  3554.             bits-per-pixel)))
  3555.       (declare (type array-index pixarray-padded-bits-per-line
  3556.              pixarray-start-bit-offset))
  3557.       (when (if (eq *computed-image-byte-lsb-first-p* *computed-image-bit-lsb-first-p*)
  3558.         (and (index-zerop (index-mod pixarray-padded-bits-per-line 8))
  3559.              (index-zerop (index-mod pixarray-start-bit-offset 8)))
  3560.           (and (index-zerop (index-mod pixarray-padded-bits-per-line *image-unit*))
  3561.            (index-zerop (index-mod pixarray-start-bit-offset *image-unit*))))
  3562.     (multiple-value-bind (image-swap-function image-swap-lsb-first-p)
  3563.         (image-swap-function
  3564.           bits-per-pixel
  3565.           *image-unit* *computed-image-byte-lsb-first-p*
  3566.           *computed-image-bit-lsb-first-p*
  3567.           unit byte-lsb-first-p bit-lsb-first-p)
  3568.       (declare (type symbol image-swap-function)
  3569.            (type boolean image-swap-lsb-first-p))
  3570.       (with-underlying-simple-vector (src card8 pixarray)
  3571.         (funcall
  3572.           (symbol-function image-swap-function)
  3573.           src bbuf (index-floor pixarray-start-bit-offset 8) boffset
  3574.           (index-ceiling (index* width bits-per-pixel) 8)
  3575.           (index-floor pixarray-padded-bits-per-line 8)
  3576.           padded-bytes-per-line height image-swap-lsb-first-p))
  3577.       t)))))
  3578.  
  3579. (defun fast-write-pixarray (bbuf boffset pixarray x y width height
  3580.                 padded-bytes-per-line bits-per-pixel
  3581.                 unit byte-lsb-first-p bit-lsb-first-p)
  3582.   (declare (type buffer-bytes bbuf)
  3583.        (type pixarray pixarray)
  3584.        (type card16 x y width height)
  3585.        (type array-index boffset padded-bytes-per-line)
  3586.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3587.        (type (member 8 16 32) unit)
  3588.        (type boolean byte-lsb-first-p bit-lsb-first-p))
  3589.   (progn bbuf boffset pixarray x y width height padded-bytes-per-line
  3590.      bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  3591.   (or
  3592.     #+(or Genera lcl3.0 excl)
  3593.     (fast-write-pixarray-with-swap
  3594.       bbuf boffset pixarray x y width height padded-bytes-per-line
  3595.       bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  3596.     (let ((function
  3597.         (or #+lispm
  3598.         (and (= (sys:array-element-size pixarray) bits-per-pixel)
  3599.              (zerop (index-mod padded-bytes-per-line 4))
  3600.              (zerop (index-mod
  3601.                   (* #+Genera (sys:array-row-span pixarray)
  3602.                  #-Genera (array-dimension pixarray 1)
  3603.                  bits-per-pixel)
  3604.                   32))
  3605.              #'fast-write-pixarray-using-bitblt)
  3606.         #+CMU
  3607.         (and (index= (pixarray-element-size pixarray) bits-per-pixel)
  3608.              #'fast-write-pixarray-using-bitblt)
  3609.         #+(or lcl3.0 excl)
  3610.         (and (index= bits-per-pixel 1)
  3611.              #'fast-write-pixarray-1)
  3612.         #+(or lcl3.0 excl)
  3613.         (and (index= bits-per-pixel 4)
  3614.              #'fast-write-pixarray-4)
  3615.         #+(or Genera lcl3.0 excl CMU)
  3616.         (and (index= bits-per-pixel 24)
  3617.              #'fast-write-pixarray-24))))
  3618.       (when function
  3619.     (write-pixarray-internal
  3620.       bbuf boffset pixarray x y width height padded-bytes-per-line
  3621.       bits-per-pixel function
  3622.       *image-unit* *image-byte-lsb-first-p* *image-bit-lsb-first-p*
  3623.       unit byte-lsb-first-p bit-lsb-first-p)))))
  3624.  
  3625. ;;; FAST-COPY-PIXARRAY - copy part of a pixarray into another
  3626.  
  3627. (defun fast-copy-pixarray (pixarray copy x y width height bits-per-pixel)
  3628.   (declare (type pixarray pixarray copy)
  3629.        (type card16 x y width height)
  3630.        (type (member 1 4 8 16 24 32) bits-per-pixel))
  3631.   (progn pixarray copy x y width height bits-per-pixel nil)
  3632.   (or
  3633.     #+(or lispm CMU)
  3634.     (let* ((pixarray-padded-pixels-per-line
  3635.          #+Genera (sys:array-row-span pixarray)
  3636.          #-Genera (array-dimension pixarray 1))
  3637.        (pixarray-padded-bits-per-line
  3638.          (* pixarray-padded-pixels-per-line bits-per-pixel))
  3639.        (copy-padded-pixels-per-line
  3640.          #+Genera (sys:array-row-span copy)
  3641.          #-Genera (array-dimension copy 1))
  3642.        (copy-padded-bits-per-line
  3643.          (* copy-padded-pixels-per-line bits-per-pixel)))
  3644.       #-CMU
  3645.       (when (and (= (sys:array-element-size pixarray) bits-per-pixel)
  3646.          (zerop (index-mod pixarray-padded-bits-per-line 32))
  3647.          (zerop (index-mod copy-padded-bits-per-line 32)))
  3648.     (sys:bitblt boole-1 width height pixarray x y copy 0 0)
  3649.     t)
  3650.       #+CMU
  3651.       (when (index= (pixarray-element-size pixarray)
  3652.             (pixarray-element-size copy)
  3653.             bits-per-pixel)
  3654.     (copy-bit-rect pixarray pixarray-padded-bits-per-line x y
  3655.                copy copy-padded-bits-per-line 0 0
  3656.                height width)
  3657.     t))
  3658.     
  3659.     #+(or lcl3.0 excl)
  3660.     (unless (index= bits-per-pixel 24)
  3661.       (let ((pixarray-padded-bits-per-line
  3662.           (if (index= height 1) 0
  3663.         (index* (index- (array-row-major-index pixarray 1 0)
  3664.                 (array-row-major-index pixarray 0 0))
  3665.             bits-per-pixel)))
  3666.         (copy-padded-bits-per-line
  3667.           (if (index= height 1) 0
  3668.         (index* (index- (array-row-major-index copy 1 0)
  3669.                 (array-row-major-index copy 0 0))
  3670.             bits-per-pixel)))
  3671.         (pixarray-start-bit-offset
  3672.           (index* (array-row-major-index pixarray y x)
  3673.               bits-per-pixel)))
  3674.     (declare (type array-index pixarray-padded-bits-per-line
  3675.                copy-padded-bits-per-line pixarray-start-bit-offset))
  3676.     (when (if (eq *computed-image-byte-lsb-first-p* *computed-image-bit-lsb-first-p*)
  3677.           (and (index-zerop (index-mod pixarray-padded-bits-per-line 8))
  3678.                (index-zerop (index-mod copy-padded-bits-per-line 8))
  3679.                (index-zerop (index-mod pixarray-start-bit-offset 8)))
  3680.         (and (index-zerop (index-mod pixarray-padded-bits-per-line *image-unit*))
  3681.              (index-zerop (index-mod copy-padded-bits-per-line *image-unit*))
  3682.              (index-zerop (index-mod pixarray-start-bit-offset *image-unit*))))
  3683.       (with-underlying-simple-vector (src card8 pixarray)
  3684.         (with-underlying-simple-vector (dst card8 copy)
  3685.           (image-noswap
  3686.         src dst
  3687.         (index-floor pixarray-start-bit-offset 8) 0
  3688.         (index-ceiling (index* width bits-per-pixel) 8)
  3689.         (index-floor pixarray-padded-bits-per-line 8)
  3690.         (index-floor copy-padded-bits-per-line 8)
  3691.         height nil)))
  3692.       t)))
  3693.     #+(or lcl3.0 excl)
  3694.     (macrolet
  3695.       ((copy (type element-type)
  3696.      `(let ((pixarray pixarray)
  3697.         (copy copy))
  3698.         (declare (type ,type pixarray copy))
  3699.         #.(declare-buffun)
  3700.         (with-underlying-simple-vector (src ,element-type pixarray)
  3701.           (with-underlying-simple-vector (dst ,element-type copy)
  3702.         (do* ((dst-y 0 (index1+ dst-y))
  3703.               (src-y y (index1+ src-y)))
  3704.              ((index>= dst-y height))
  3705.           (declare (type card16 dst-y src-y))
  3706.           (do* ((dst-idx (array-row-major-index copy dst-y 0)
  3707.                  (index1+ dst-idx))
  3708.             (dst-end (index+ dst-idx width))
  3709.             (src-idx (array-row-major-index pixarray src-y x)
  3710.                  (index1+ src-idx)))
  3711.                ((index>= dst-idx dst-end))
  3712.             (declare (type array-index dst-idx src-idx dst-end))
  3713.             (setf (aref dst dst-idx)
  3714.               (the ,element-type (aref src src-idx))))))))))
  3715.       (ecase bits-per-pixel
  3716.     (1  (copy pixarray-1  pixarray-1-element-type))
  3717.     (4  (copy pixarray-4  pixarray-4-element-type))
  3718.     (8  (copy pixarray-8  pixarray-8-element-type))
  3719.     (16 (copy pixarray-16 pixarray-16-element-type))
  3720.     (24 (copy pixarray-24 pixarray-24-element-type))
  3721.     (32 (copy pixarray-32 pixarray-32-element-type)))
  3722.       t)))
  3723.